write a menu driven program to accept a number from the user whether it is a palindrome or a perfect number
Answers
Answer:
// Write a menu driven class to accept a number from the user and check whether it is a Palindrome or a Perfect number.
// (a)Palindrome number – a number is Palindrome which when read in reverse order is same as read in the right order. Example 11, 101, 151 etc.
// (b)Perfect number – a number is called Perfect if it is equal to the sum of its factors other than the number itself. Example 6 = 1 + 2 + 3
//VARIABLE DESCRIPTION:
//CH=CHOICE OF USER
//NUM=USER INPUT FOR NUMBER
//RUN=INITIALISATION TO CHECK WHETHER FIRST OR SECOND TIME RUNNING IN THAT FUNCTION
//REV=STORES REVERSE CHARACTERS ONE BY ONE
//NUM1=STORES BACKUP FOR FIRST INPUT(NUM)
//D=SPLITS EACH CHARACTER ONE BY ONE
//I=STORES FACTORS
import java.io.*;
class PALINDROME_OR_PERFECT
{
private static int run;
private static int num;
private static int ch;
public static void main(String args[])throws IOException
{
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
System.out.println("ENTER YOUR CHOICE :");
System.out.println("1 FOR PALINDROME ");
System.out.println("2 FOR PERFECT NUMBER ");
ch=Integer.parseInt(input.readLine());
if(ch==1)
{
System.out.println("ENTER A NUMBER FOR FINDING WHETHER IT IS A PALINDROME");
num=Integer.parseInt(input.readLine());
int d;
int rev=0;
int num1=num;
do
{
d=num%10;
rev=(rev*10)+d;
num=num/10;
}while(num!=0);
if(num1==rev)
System.out.println("IT IS A PALINDROME");
else
System.out.println("IT IS NOT A PALINDROME");
}
else if(ch==2)
{
System.out.println("ENTER A NUMBER FOR FINDING WHETHER IT IS A PERFECT NUMBER");
num=Integer.parseInt(input.readLine());
int i=1;
int sum=0;
while(i<num)
{
if(num%i==0)
sum=sum+i;
i++;
}
if(sum==num)
System.out.println("IT IS A PERFECT NUMBER");
else
System.out.println("IT IS NOT A PERFECT NUMBER");
}
else
System.out.println("ENTERED WRONG DATA");
}
}
OUTPUT 1:
ENTER YOUR CHOICE :
1 FOR PALINDROME
2 FOR PERFECT NUMBER
1
ENTER A NUMBER FOR FINDING WHETHER IT IS A PALINDROME
33
IT IS A PALINDROME
OUTPUT 2:
ENTER YOUR CHOICE :
1 FOR PALINDROME
2 FOR PERFECT NUMBER
1
ENTER A NUMBER FOR FINDING WHETHER IT IS A PALINDROME
67
IT IS NOT A PALINDROME
OUTPUT 3:
ENTER YOUR CHOICE :
1 FOR PALINDROME
2 FOR PERFECT NUMBER
2
ENTER A NUMBER FOR FINDING WHETHER IT IS A PERFECT NUMBER
6
IT IS A PERFECT NUMBER
OUTPUT 4:
ENTER YOUR CHOICE :
1 FOR PALINDROME
2 FOR PERFECT NUMBER
2
ENTER A NUMBER FOR FINDING WHETHER IT IS A PERFECT NUMBER
3
IT IS NOT A PERFECT NUMBER
OUTPUT 5:
ENTER YOUR CHOICE :
1 FOR PALINDROME
2 FOR PERFECT NUMBER
6
ENTERED WRONG DATA