Write a menu driven program to accept a number from the user and check whether it is a palindrome number or a perfect number
Answers
Answer:
IMPORT JAVA.UTIL.*;
CLASS MENU1
{
VOID MAIN()
{
SCANNER SC=NEW SCANNER(SYSTEM.IN);
SYSTEM.OUT.PRINTLN("1. CHECK NUMBER FOR PALINDROME");
SYSTEM.OUT.PRINTLN("2. CHECK NUMBER FOR PERFECT");
SYSTEM.OUT.PRINTLN("ENTER YOUR CHOICE");
INT CH=SC.NEXTINT();
SYSTEM.OUT.PRINTLN("ENTER NUMBER");
INT N=SC.NEXTINT();
SWITCH(CH)
{
CASE 1:
INT R=0,D,BK=N,
WHILE(BK!=0)
{
D=BK%10;
R=R*10+D;
BK=BK/10;
}
IF(R==N)
SYSTEM.OUT.PRINTLN("NUMBER IS PALINDROME");
ELSE
SYSTEM.OUT.PRINTLN("NUMBER IS NOT PALINDROME");
}
BREAK;
CASE 2;
INT I,S=0,
FOR(I=0;I<N;I++)
{
IF(N%I==0)
S=S+I;
}
IF(S==N)
SYSTEM.OUT.PRINTLN("NUMBER IS PERFECT");
ELSE
SYSTEM.OUT.PRINTLN("NUMBER IS NOT PERFECT");
BREAK;
DEFAULT:
SYSTEM.OUT.PRINTLN("INVALID CHOICE");
}
}
}
Explanation:
Answer:
import java.util.*;
import java.io.*;
public class Program{
public static void main(String[] args) throws IOException{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
System.out.println("1. check number for palindrome");
System.out.println("2. check number for perfect");
System.out.println("enter your choice");
int ch=Integer.parseInt(in.readLine());
System.out.println("enter number");
int n=Integer.parseInt(in.readLine());
switch(ch){
case 1:
int r=0,d,bk=n;
for(;bk!=0;){
d=bk%10;
r=r*10+d;
bk = bk/10;
}
if(r==n)
System.out.println("number is palindrome");
else
System.out.println("number is not palindrome");
break;
case 2:
int i,s=0;
for(i=1;i<n;i++){
if(n%i==0)
s=s+i;
}
if(s==n)
System.out.println("number is perfect");
else
System.out.println("number is not perfect");
break;
default:
System.out.println("invalid choice");
}}}
Explanation: