Page 219 - CA_Blue( J )_Class9
P. 219
Program 8 Write a program to demonstrate if a number is a palindrome or not.
1 import java.util.*;
2 class palindrome
3 {
4 public static void main(String args[])
5 {
6 Scanner k = new Scanner(System.in);
7 int nOrg, numReverse = 0;
8 System.out.print("Enter a number to check: ");
9 int num= k.nextInt ();
10 nOrg = num;
11 while (num > 0)
12 {
13 int digit = (int )num % 10;
14 numReverse = numReverse * 10 + digit;
15 num = num / 10;
16 }
17 if (numReverse == nOrg)
18 System.out.println(nOrg + " is a Palindrome.");
19 else
20 System.out.println(nOrg + " is a not Palindrome.");
21 }
22 }
You will get the following output:
Iterative Constructs in Java 217

