Chatbox

Các bạn vui lòng dùng từ ngữ lịch sự và có văn hóa,sử dụng Tiếng Việt có dấu chuẩn. Chúc các bạn vui vẻ!
21/10/2021 11:10 # 1
buiducduong
Cấp độ: 22 - Kỹ năng: 1

Kinh nghiệm: 17/220 (8%)
Kĩ năng: 0/10 (0%)
Ngày gia nhập: 25/09/2020
Bài gởi: 2327
Được cảm ơn: 0
Palindrome Number Program in Java Using while & for Loop


 

What is Palindrome Number?

Palindrome number is a number that remains the same number when it is reversed. For example, 131. When its digits are reversed, it remains the same number. Palindrome number has reflection symmetry at the vertical axis. It refers to the word which has the same spelling when its letters are reversed.

Examples of Palindrome Number in Java

121, 393, 34043, 111, 555, 48084

Examples of Palindrome Number

 LOL, MADAM

Palindrome Number Algorithm

Below is Palindrome number algorithm logic in Java:

  • Fetch the input number that needs to be checked for being a Palindrome
  • Copy number into a temporary variable and reverse it.
  • Compare the reversed and original number.
  • If they are same, number is “palindrome number”
  • Else number is not “palindrome number”

How to check whether input number is Palindrome or not

Below is a Palindrome program in Java with while loop

package com.guru99;
 
public class PalindromeNum {
 
	public static void main(String[] args)
	{
 
		int lastDigit,sum=0,a;    
		int inputNumber=171; //It is the number  to be checked for palindrome 
 
		a=inputNumber; 
        
        // Code to reverse a number
		while(a>0)
		{   System.out.println("Input Number "+a);  
			lastDigit=a%10; //getting remainder  
			System.out.println("Last Digit "+lastDigit); 
			System.out.println("Digit "+lastDigit+ " was added to sum "+(sum*10)); 
			sum=(sum*10)+lastDigit;  
			a=a/10;
			
		}    
 
		// if given number equal to sum than number is palindrome otherwise not palindrome
		if(sum==inputNumber)    
			System.out.println("Number is palindrome ");    
		else    
			System.out.println("Number is not palindrome");    
 
	}
 
}

Code Output:

Input Number 171
Last Digit 1
Digit 1 was added to sum 0
Input Number 17
Last Digit 7
Digit 7 was added to sum 10
Input Number 1
Last Digit 1
Digit 1 was added to sum 170
Number is palindrome

Program to Check Palindrome using for loop

Below is a Java program for Palindrome using for loop

package com.guru99;
 
public class PalindromeNum {
 
	public static void main(String[] args)
	{
 
		int lastDigit,sum=0,a;    
		int inputNumber=185; //It is the number  to be checked for palindrome 
 
		a=inputNumber; 
        
        // Code to reverse a number
	for( ;a != 0; a /= 10 )
		{   System.out.println("Input Number "+a);  
			lastDigit=a%10; //getting remainder  
			System.out.println("Last Digit "+lastDigit); 
			System.out.println("Digit "+lastDigit+ " was added to sum "+(sum*10)); 
			sum=(sum*10)+lastDigit;  
			a=a/10;
			
		}    
 
		// if given number equal to sum than number is palindrome otherwise not palindrome
		if(sum==inputNumber)    
			System.out.println("Number is palindrome ");    
		else    
			System.out.println("Number is not palindrome");    
 
	}
 
}

Code Output:

Input Number 185
Last Digit 5
Digit 5 was added to sum 0
Input Number 1
Last Digit 1
Digit 1 was added to sum 50
Number is not palindrome

What is Palindrome Number?

Palindrome number is a number that remains the same number when it is reversed. For example, 131. When its digits are reversed, it remains the same number. Palindrome number has reflection symmetry at the vertical axis. It refers to the word which has the same spelling when its letters are reversed.

Examples of Palindrome Number in Java

121, 393, 34043, 111, 555, 48084

Examples of Palindrome Number

 LOL, MADAM

Palindrome Number Algorithm

Below is Palindrome number algorithm logic in Java:

  • Fetch the input number that needs to be checked for being a Palindrome
  • Copy number into a temporary variable and reverse it.
  • Compare the reversed and original number.
  • If they are same, number is “palindrome number”
  • Else number is not “palindrome number”

How to check whether input number is Palindrome or not

Below is a Palindrome program in Java with while loop

package com.guru99;
 
public class PalindromeNum {
 
	public static void main(String[] args)
	{
 
		int lastDigit,sum=0,a;    
		int inputNumber=171; //It is the number  to be checked for palindrome 
 
		a=inputNumber; 
        
        // Code to reverse a number
		while(a>0)
		{   System.out.println("Input Number "+a);  
			lastDigit=a%10; //getting remainder  
			System.out.println("Last Digit "+lastDigit); 
			System.out.println("Digit "+lastDigit+ " was added to sum "+(sum*10)); 
			sum=(sum*10)+lastDigit;  
			a=a/10;
			
		}    
 
		// if given number equal to sum than number is palindrome otherwise not palindrome
		if(sum==inputNumber)    
			System.out.println("Number is palindrome ");    
		else    
			System.out.println("Number is not palindrome");    
 
	}
 
}

Code Output:

Input Number 171
Last Digit 1
Digit 1 was added to sum 0
Input Number 17
Last Digit 7
Digit 7 was added to sum 10
Input Number 1
Last Digit 1
Digit 1 was added to sum 170
Number is palindrome

Program to Check Palindrome using for loop

Below is a Java program for Palindrome using for loop

package com.guru99;
 
public class PalindromeNum {
 
	public static void main(String[] args)
	{
 
		int lastDigit,sum=0,a;    
		int inputNumber=185; //It is the number  to be checked for palindrome 
 
		a=inputNumber; 
        
        // Code to reverse a number
	for( ;a != 0; a /= 10 )
		{   System.out.println("Input Number "+a);  
			lastDigit=a%10; //getting remainder  
			System.out.println("Last Digit "+lastDigit); 
			System.out.println("Digit "+lastDigit+ " was added to sum "+(sum*10)); 
			sum=(sum*10)+lastDigit;  
			a=a/10;
			
		}    
 
		// if given number equal to sum than number is palindrome otherwise not palindrome
		if(sum==inputNumber)    
			System.out.println("Number is palindrome ");    
		else    
			System.out.println("Number is not palindrome");    
 
	}
 
}

Code Output:

Input Number 185
Last Digit 5
Digit 5 was added to sum 0
Input Number 1
Last Digit 1
Digit 1 was added to sum 50
Number is not palindrome



 
Copyright© Đại học Duy Tân 2010 - 2024