Computer Science, asked by josephfranky980, 9 months ago

Write a menu driven program to accept a number and check whether it is a prime number or
a perfect number as per user's choice.​

Answers

Answered by Brenquoler
28

(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!!!");

} } }

Similar questions