Computer Science, asked by pk240413dh, 10 months ago

weite a menu driven program to accept a number and cheack weather it is a prime number or a perfect number as per the users choice​

Answers

Answered by shilpa2527
3

Explanation:

import java.util.*;

class number{

static void f(){

Scanner sc=new Scanner(System.in);

System.out.println("Enter 1:Prime number \t 2:Neon number \t 3:Perfect number"); //Write these in 1line only

int choice=sc.nextInt();

switch(choice){

case 1:

System.out.println("Enter a number:");

int n=sc.nextInt();

int a,c=0;

for(a=1;a<=n;a++){

c++;

}

if(c%2==0){

System.out.println("It is a prime number");

}

else

System.out.println("It is a prime number");

break;

case 2:

System.out.println("Enter a number:");

int num=sc.nextInt();

int temp=num;

int d,sum=0;

int sq=num*num;

while(sq>0)

{

d=sq%10;

sq=sq/10;

sum=sum+d;

}

if(sum==temp)

System.out.println("It is a neon number");

else

System.out.println("It is not a neon number");

break;

case 3:

System.out.println("Enter a number:");

int number=sc.nextInt();

int t=number;

int x,s=0;

for(x=1;x<number;x++){

if(number%x==0)

s=s+x;

}

if(s==t)

System.out.println("It is a perfect number");

else

System.out.println("It is not a perfect number");

break;

}}}

please mark me as brainliest ʘ‿ʘ

Answered by Brenquoler
233

(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