Java Program To Check A Number is Palindrome or Not
Palindrome Number
Take a number and reverse it. If the number remains same after reversing then the number is called a palindrome number. But if the number comes different from the original number after reversing then the number is not a palindrome number.
Example: 1221 it’s a palindrome number
Original number: 1221 after reversing: 1221
1874 it’s not a palindrome number.
Original number: 1874 after reversing: 4781
Check If A String Is Palindrome Or Not Ignoring The Case Of Characters In Java
Algorithm for checking a number is palindrome or not
Step 1: Take a number as input from the user.
Step 2: Put the number in another variable temporarily.
Step 3: Reverse the number using while loop. ( number%10—- get the remainder. Initialize check variable with zero, then in each iteration of while loop just assign check=check*10+remainder and assign n=n/10. by using n=n/10 we can finally reach at that point when n will be zero, at that moment while loop will be closed. Then we can compare the value of the original number with check variable. Check is the value of the reversed number)
Step 4: If both numbers are equal then it’s a palindrome number otherwise it’s not a palindrome number.
Java code to check a number is palindrome number or not
import java.util.Scanner; public class Palindrome { public static void main(String []args) { Scanner input=new Scanner(System.in); System.out.println("Enter a Number:"); int n=input.nextInt(); int rev=n; int original=n; int remainder; int check=0; while(n>0) { remainder=n%10; check=(check*10)+remainder; n=n/10; } if(rev==check){ System.out.println(original+" Is a palindrome number"); } else{ System.out.println(original+" Is not palindrome number"); } } }
Java Program To Check A Number Is Prime or Not
Output:
run: Enter a Number: 1225221 1225221 Is a palindrome number BUILD SUCCESSFUL (total time: 11 seconds)
run: Enter a Number: 154623 154623 Is not palindrome number BUILD SUCCESSFUL (total time: 2 seconds)
Leave a Reply