java program for checking palindrome number
Palindrome Program in Java
In this program, you’ll learn to check whether a number is palindrome or not in Java. This is done by using for and while loop.
Palindrome number in java:
A palindrome number is a number that is same after reverse. For example 141, 757, 38583, 747, 171, 52925 are the palindrome numbers. It can also be a string like LOL, NITIN etc.
Palindrome number algorithm
- Get the number to check for palindrome
- Hold the number in temporary variable
- Reverse the number
- Compare the temporary number with reversed number
- If both numbers are same, print “palindrome number”
- Else print “not palindrome number”
Java program for checking palindrome number :
import java.io.* ; import java.util.Scanner; class CheckPalindrome { public static void main (String s[] ) { Scanner console = new Scanner(System.in); int number ; System.out.println( " Enter the array size " ); int size = console.nextInt(); int array[] = new int[size]; for ( int i=0; i<size;i++) { System.out.println ( "Enter the number : array["+i+"]"); number = console.nextInt() ; array[i] = number ; } int c = array.length-1 ; for ( int i = 0; i < array.length ; i++) { if ( array[i] == array[c]) { c--; } } if (c == (-1) ) { System.out.println ( " this array is a palindrome "); } else {System.out.println( " this is not a palindrome " ); } System.out.println( " this is the second method to solve palindrome " ); int flag1 = 1; for(int i = 0, k = array.length-1; i<array.length&k>(-1);i++,k--) { if ( array[i] != array[k]) { System.out.println ("this is not a palindrome"); flag1 = 0; break ; } } if (flag1==1){ System.out.println( " this is a palindrome " ); } } }