0% found this document useful (0 votes)
94 views2 pages

Java Menu Driven

The program provides a menu driven calculator application that allows a user to select one of four options: 1) Calculate the factorial of a number, 2) Calculate an exponential expression, 3) Determine if a number is prime, or 4) Exit the application. The user's selection is processed using a switch statement to call the appropriate logic to perform the calculation or check and display the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views2 pages

Java Menu Driven

The program provides a menu driven calculator application that allows a user to select one of four options: 1) Calculate the factorial of a number, 2) Calculate an exponential expression, 3) Determine if a number is prime, or 4) Exit the application. The user's selection is processed using a switch statement to call the appropriate logic to perform the calculation or check and display the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

* My Program
*/
package javaapplication4;

import java.util.Scanner;

public class JavaApplication4 {

public static void main(String[] args) {

//menu program

int option;
int fact=1,number;
int exponent;
float base,power=1;
boolean prime=true;

Scanner inputData = new Scanner(System.in);

do{
//menu options
System.out.print(" 1.FATORIAL \n");
System.out.print(" 2.EXPONENTIAL \n");
System.out.print(" 3.PRIME NUMBER \n");
System.out.print(" 4.EXIT APP \n");
System.out.print("Choice:");
option = inputData.nextInt();

//SWITCH CASE TO SELECT CHOICE AND DO CALCULATION


switch(option){
case 1:

if(option==1){
System.out.print("FACTORIAL \n");
System.out.print("Enter value of n \n");
number=inputData.nextInt();
//Loop to calc the answer(FACTORIAL)
for( int i=1; i<=number;i++){
fact=fact*i;
}

System.out.println("Factorial of "+number+" is: "+fact);


}
break;

case 2:
if(option==2){
System.out.print("EXPONENT \n");
System.out.print("Enter Base Value(X) \n");
base=inputData.nextFloat();
System.out.print("Enter Exponent(Y) \n");
exponent=inputData.nextInt();

int i=0;
while(i!=exponent){
power=power*base;
i++;
}
System.out.println("Answer is:"+power);
}
break;

case 3:
if(option==3){
int R,i=2;
System.out.print("PRIME NUMBER \n");
System.out.print("Enter Value of R \n");
R=inputData.nextInt();

while(i<=R/2){

if(R%i==0){
prime=false;
break;
}
i++;
}
if(prime){
System.out.print(R+" Is Prime Number \n");
}else{
System.out.print(R+" Is not Prime Number \n");
}
}

break;

case 4:
if(option!=4){
return;
}
break;

default:
System.out.print("Ivalid Option! \n");

}while(option!=4);
}

You might also like