Write a program to input a number from the user and print whether it is a palindrome number or not as follows.
int palindrome(int p): To check and return whether the number is a palindrome number or not.(A number which is read same from both the sides is called palindrome number. E.g. 121, 454, 12321, 565…etc.) The function returns 1(one) if the number is palindrome otherwise returns 0(zero)
void main(): To input a number from the user and print whether it is a palindrome number or not using a returned type method called int palindrome(int n)in java
Answers
The given problem is solved using Java.
import java.util.*;
public class PalindromeChecker{
static int palindrome(int p){
int n=p,s=0;
for(;n!=0;n/=10)
s=s*10+n%10;
if(p==s)
return 1;
return 0;
}
public static void main(String s[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=sc.nextInt();
if(palindrome(n)==1)
System.out.println("Number is Palindrome.");
else
System.out.println("Number is not Palindrome.");
}
}
The function palindrome(p) returns 1 if the number is Palindrome or else, it returns 0.
The function is then called inside main. If the function returns 1, then the message - "Palindrome" is displayed on the screen or else, the message "Not Palindrome." is displayed on the screen.
See attachment for output.