Write a Java Program to make a Menu Driven Calculator
import java.util.Scanner; public class CalciMenuDriven { public static void main(String[] args) { int n1,n2; int ch=0,ans = 0; int choice; Scanner scan = new Scanner(System.in); System.out.println("Enter First Number: "); n1=scan.nextInt(); System.out.println("Enter Second Number: "); n2=scan.nextInt(); do { System.out.println("Enter Your Choice 1. Addition 2.Substraction 3. Multiplication 4. Division"); choice=scan.nextInt(); switch(choice) { case 1 : ans=n1+n2; System.out.println(n1+" "+ "+" +" "+n2+" = "+ans); break; case 2 : ans=n1-n2; System.out.println(n1+" "+ "-" +" "+n2+" = "+ans); break; case 3 : ans=n1*n2; System.out.println(n1+" "+ "*" +" "+n2+" = "+ans); break; case 4 : ans=n1/n2; System.out.println(n1+" "+ "/" +" "+n2+" = "+ans); break; default: System.out.println("Wrong input...."); System.out.println("Exit..."); return; } }while(choice!=5); } }
Output:
Enter First Number:
50
Enter Second Number:
10
Enter Your Choice
1. Addition
2. Substraction
3. Multiplication
4. Division
1
50 + 10 = 60
Enter Your Choice
1. Addition
2. Substraction
3. Multiplication
4. Division
2
50 - 10 = 40
Enter Your Choice
1. Addition
2. Substraction
3. Multiplication
4. Division
3
50 * 10 = 500
Enter Your Choice
1. Addition
2. Substraction
3. Multiplication
4. Division
4
50 / 10 = 5
Enter Your Choice
1. Addition
2. Substraction
3. Multiplication
4. Division
5
Wrong input....
Exit...
No comments:
Post a Comment
If you have any doubts, please let me know