Palindrome Number Program in Java using While loop
ADVERTISEMENTS
Palindrome number program in java using while loop. In this article, you will learn how to make a palindrome number program in java using while loop.
What is Palindrome Number?
A palindrome is a case where if the reverse of any number will be matched correctly with its original integer number, It will be a palindrome.
Palindrome Number Formula
7997 == 7997 => a palindrome
Source Code
// Palindrome Number Program in Java using While loop
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter an integer number::\n");
int x = in.nextInt(),
r,
rN = 0,
oN;
// rN - To store the reverse number
// oN - To store the original number
// r - To store the remainder
oN = x;
// this reverse number will be stored during iteration
while (x != 0) {
r = x % 10;
rN = rN * 10 + r;
x /= 10;
}
// if the original number will match with reverse then palindrome case will be true
if (oN == rN) {
System.out.println ("This " + oN + " is a palindrome number.");
} else {
System.out.println ("This " + oN + " is not a palindrome number!");
}
}
}
Output
Enter an integer number::
90009
This 90009 is a palindrome number.