Design a class to input a number and print whether it is a palindrome or not.
java while loop and for loop
Answers
Answered by
3
Question:-
Design a class to input a number and print whether it is palindrome or not.
Program:-
In Java.
Using while loop.
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=sc.nextInt();
int m=n, s=0;
while(n!=10)
{
s=s*10+n%10;
n/=10;
}
if(s==m)
System.out.println("Palindrome.");
else
System.out.println("Not Palindrome. ");
}
}
Using for loop.
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=sc.nextInt();
int m=n, s=0;
for(;n!=10;n/=10)
s=s*10+n%10;
if(s==m)
System.out.println("Palindrome.");
else
System.out.println("Not Palindrome. ");
}
}
Similar questions