Write a menu driven program(based on user's choice)
a) find factorial of a number and
b) To check the number is prime number or not.
Answers
Answered by
4
Answer:
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch(choice){
case 1:
int num = sc.nextInt();
long factorial = 1;
for(int i = 1; i <= num; ++i)
{
// factorial = factorial * i;
factorial *= i;
}
System.out.printf("Factorial =" + factorial); break;
case 2:
int num, i, count=0;
System.out.print("Enter a Number : ");
num = sc.nextInt();
for(i=2; i<num; i++)
{
if(num%i == 0)
{
count++;
break;
}
}
if(count == 0)
{
System.out.print("This is a Prime Number");
}
else
{
System.out.print("This is not a Prime Number");
} break;
}
}
}
Similar questions
Computer Science,
2 months ago
Physics,
2 months ago
Math,
4 months ago
Math,
10 months ago
English,
10 months ago