Define a class MENU to perform the following operations depending upon the user’s choice(using switch-case) (i) Print absolute value of a number (ii) Print rounded value of a number (iii) Print n raised to power x (iv) Quit
import java.util.*;
class MENU
{
public static void main()
{
Scanner Sc=new Scanner(System.in);
class MENU
{
public static void main()
{
Scanner Sc=new Scanner(System.in);
System.out.println(“Enter 1 to print the absolute value of a
number”);
System.out.println(“Enter 2 to print the rounded up value of a number”);
System.out.println(“Enter 3 to print n raised to power x”);
System.out.println(“Enter 4 to quit”);
int ch=Sc.nextInt();
System.out.println(“Enter 2 to print the rounded up value of a number”);
System.out.println(“Enter 3 to print n raised to power x”);
System.out.println(“Enter 4 to quit”);
int ch=Sc.nextInt();
Switch(ch)
case 1:System.out.println(“Enter a no. with decimal point in it”);
int a=Sc.nextInt();
{ System.out.println(“absolute number “+(Math.abs(a))); } break;
case 2: System.out.println(“Enter a decimal point containing no.”);
double b=Sc.nextInt();
{
System.out.println(“Rounded up number “+(Math.round(b)));
}
break;
case 3: System.out.println(“Enter two numbers”);
double c=Sc.nextDouble();
double d=Sc.nextDouble();
{
System.out.println(“n to the power x is “+(Math.pow(n,x))); } break;
case 4: System.out.println(“No work to be done so the program is terminated”);
}
default :{ System.out.println(“Wrong input”);
} } }
Output: Enter
1 to print the absolute value of a no. case 1:System.out.println(“Enter a no. with decimal point in it”);
int a=Sc.nextInt();
{ System.out.println(“absolute number “+(Math.abs(a))); } break;
case 2: System.out.println(“Enter a decimal point containing no.”);
double b=Sc.nextInt();
{
System.out.println(“Rounded up number “+(Math.round(b)));
}
break;
case 3: System.out.println(“Enter two numbers”);
double c=Sc.nextDouble();
double d=Sc.nextDouble();
{
System.out.println(“n to the power x is “+(Math.pow(n,x))); } break;
case 4: System.out.println(“No work to be done so the program is terminated”);
}
default :{ System.out.println(“Wrong input”);
} } }
Enter 2 to print the rounded up value of a no.
Enter 3 to print n raised to power x
Enter 4 to quit
2
Enter a decimal point containing no. 1.99
Rounded up number 2
No comments: