write a menu driven program to accept a number and check weather it is a prime number or a perfect number as per user's choice
(note:- a perfect number is a number which is equal to the sum of its factors excluding the number itself )
eg-1+2+3=6
Answers
Answer:
A number is perfect if the sum of its proper factors is equal to the number. To find the proper factors of a number, write down all numbers that divide the number with the exception of the number itself. If the sum of the factors is equal to 18, then 18 is a perfect number.
Pls mark me as brainlist
(Note: A perfect number is a number which is equal to the sum of its factors excluding the number itself. E.g: 6 is a perfect no as the sum of its factors 1 + 2 + 3 = 6)
Program : (Using Scanner class)
import java.util.*;
public class MenuPrimePerfect {
public static void main(String args[])
{ int ch,n,num;
Scanner inp=new Scanner(System.in);
System.out.println("M E N U ");
System.out.println("1. To check Prime no.");
System.out.println("2. To check Perfect no.");
System.out.println("3. To exit.");
System.out.println("Enter your choice :");
ch=inp.nextInt();
System.out.println("Enter one no :");
n=inp.nextInt();
switch(ch)
{ case 1: int i,c=0;
for(i=1;i<=n;i++)
{ if(n%i==0)
c++;
}
if(c==2)
System.out.println("It's a PRIME NUMBER");
else
System.out.println("It's NOT A PRIME NUMBER");
break;
case 2 : num = n;
int s=1;
for(i=2;i<=n/2;i++)
{ if(n%i==0)
s=s+i;
}
if(s==num)
System.out.println("It's a PERFECT NUMBER");
else
System.out.println("It's NOT A PERFECT NUMBER");
break;
case 3 : System.out.println("Thanks. See you again!");
break;
default : System.out.println("Input error!!!");
} } }