Using switch case write a menu driven program to accept a number and print whether is it an
Armstrong number or an exact number.
Armstrong Number: If the sum of the cube of the digits of a number is the number itself.
Example: 371=33+73+13= 27+343+1=371
Exact Number: if the sum of the factorial of the numbers is the number itself. Example: 145= 1! + 4! + 5! = 1 + 24 + 120 = 145
Answers
Program:-
import java.util.*;
public class MenuDriven
{
public static void main(String args[ ])
{
Scanner in=new Scanner(System.in);
int ch;
System.out.println("Enter 1 to check whether a number is Armstrong or not");
System.out.println("Enter 2 for checking whether a number is an exact number or not");
System.out.println("Enter your choice");
ch=in.nextInt();
switch(ch)
{
case 1:
int n,num=0,a=0,s=0;
System.out.println("Enter the number");
n=in.nextInt();
while(n!=0)
{
a=n%10;
s=s+a*a*a;
n=n/10;
}
if(s==num)
System.out.println("It is an Armstrong number");
else
System.out.println("It is not an Armstrong number");
break;
case 2:
int n1,r=0,fact=1,num1=0;
System.out.println("Enter the number");
n1=in.nextInt();
num1=n1;
while(n1!=0)
{
r=n%10;
for(int i=1;i<=r;i++)
{
fact=fact*i;
sum=sum+fact;
}
n=n/10;
}
if(sum==num1)
System.out.println("It is an exact number");
else
System.out.println("It is not an exact number");
break;
default:
System.out.println("Wrong choice");
}
}
}