In this tutorial you will learn how to write a program in Java to check a given number is palindrome or not using iteration.
Before moving directly on the writing the program
Read this: What is Palindrome Number? Write a palindrome program in C.
How our Java program will behave?
Our program will take a number as an input to check given number of.
Suppose if someone gives an input 121 then our program should print “the given number is a palindrome”.
And if someone given input 123 the our program should print “the given number is not a palindrome number”.
Java program for palindrome number using iterative method
import java.util.*;
class Main{
public static void main(String ...args){
int tempvar,remainder,reverseNum=0;
Scanner sc= new Scanner(System.in);
System.out.print("Enter number- ");
int originalNum= sc.nextInt();
tempvar = originalNum;
while (tempvar != 0) {
remainder = tempvar % 10;
reverseNum = reverseNum * 10 + remainder;
tempvar /= 10;
}
if (originalNum == reverseNum)
System.out.print("Number is palindrom");
else
System.out.print("Number is not palindrom");
}
}
Output:
