Using the Switch Statement, Write a Menu Driven Program To: (I) to Find and Display All the Factors of a Number Input by the User (Including 1 and Excluding Number Itself).
Advertisements
प्रश्न
Using the switch statement, write a menu driven program to:
(i) To find and display all the factors of a number input by the user (including 1 and excluding number itself).
Example:
Sample Input: n=15
Sample Output: 1, 3, 5.
(ii) To find and display the factorial of a number input by the user (the factorial of a non-negative integer n, denoted by n\ is the product of all integers less than or equal to n.
Example:
Sample Input: n=5
Sample Output: 5! = 1×2×3×4×5 = 120.
For an incorrect choice, an appropriate error message should be displayed.
Advertisements
उत्तर
import java.util.scanner;
class menu
{
Public static void main ( )
{
scanner s=new scanner (System.in);
System.out.println("1. factor of No.");
System.out.println("2. factorial of No.");
int ch=S.nextInt( );
System.out.println ("Enter No");
int n=S.nextInt( );
switch (ch)
{
case 1:
for (int i=1; i<=n-1; i++)
{
if(n%i==0)
System.out.print(i);
}
break;
case 2:
int F=1;
for (int i=1; i<=n; i++)
{
F=F*i;
}
System.out.println("Factorial is" +F);
break;
default:
System.out.println("Incorrect choice");
}
}
}
