Description
Menu driven program means the code will calculate based on operator until exit condition not given. Here program will run until user will give valid operator as input. In this program, user will enter operator(+,-,*,/) and two operands. It will give output based on entered operator.
C/C++
/* C Program of Menu Driven Simple Calculator Using If Else */ //Save it as MenuDrivenSimpleCalculatorUsingIfElse.c #include<stdio.h> int main(){ //Program will run until the condition will true while(1){ double firstNumber, secondNumber; char op; printf("Enter the operation"); printf("\nFor addition + "); printf("\nFor subtraction - "); printf("\nFor multiplication * "); printf("\nFor division / "); printf("\nAny other key for exit : "); //Taking operator as character as input //Give space before scanning of character to avoid termination of programm scanf(" %c",&op); //checking if entered character is any valid operator or not if(op == '+' || op == '-' || op == '*' || op == '/') { printf("Enter the first number : "); scanf("%lf",&firstNumber); printf("Enter the second number : "); scanf("%lf",&secondNumber); if(op == '+') { printf("The summation of %lf and %lf = %lf\n",firstNumber,secondNumber,(firstNumber+secondNumber)); }else if(op == '-') { printf("The subtraction of %lf and %lf = %lf\n",firstNumber,secondNumber,(firstNumber-secondNumber)); }else if(op == '*') { printf("The multiplication of %lf and %lf = %lf\n",firstNumber,secondNumber,(firstNumber*secondNumber)); }else if(op == '/') { printf("The division of %lf and %lf = %lf\n",firstNumber,secondNumber,(firstNumber/secondNumber)); }else{ printf("Invalid Input"); } } //Program will terminate if no valid operator given as input else { printf("Successful exit"); break; } } return 0; }
Input: Enter the operation For addition + For subtraction - For multiplication * For division / Any other key for exit : + Enter the first number : 4 Enter the second number : 8 The summation of 4.0 and 8.0 = 12.0 Enter the operation For addition + For subtraction - For multiplication * For division / Any other key for exit : Output: The summation of 4.0 and 8.0 = 12.0
Java
/* Java Program of Menu Driven Simple Calculator Using If Else */ //Save it as MenuDrivenSimpleCalculatorUsingIfElse.java import java.io.*; import java.util.Scanner; public class MenuDrivenSimpleCalculatorUsingIfElse { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //Program will run until the condition will true while (true) { System.out.println("Enter the operation"); System.out.println("For addition + "); System.out.println("For subtraction - "); System.out.println("For multiplication * "); System.out.println("For division / "); System.out.println("Any other key for exit : "); //Taking operator as character as input char op = scanner.next().charAt(0); //checking if entered character is any valid operator or not if (op == '+' || op == '-' || op == '*' || op == '/') { System.out.println("Enter the first number : "); double firstNumber = scanner.nextDouble(); System.out.println("Enter the second number : "); double secondNumber = scanner.nextDouble(); if (op == '+') { System.out.println("The summation of " + firstNumber + " and " + secondNumber + " = " + (firstNumber + secondNumber)); } else if (op == '-') { System.out.println("The subtraction of " + firstNumber + " and " + secondNumber + " = " + (firstNumber - secondNumber)); } else if (op == '*') { System.out.println("The multiplication of " + firstNumber + " and " + secondNumber + " = " + (firstNumber * secondNumber)); } else if (op == '/') { System.out.println("The division of " + firstNumber + " and " + secondNumber + " = " + (firstNumber / secondNumber)); }else { System.out.println("Please enter valid operator"); } //Program will terminate if no valid operator given as input } else { System.out.println("Successful exit"); break; } } } }
Input: Enter the operation For addition + For subtraction - For multiplication * For division / Any other key for exit : + Enter the first number : 4 Enter the second number : 8 The summation of 4.0 and 8.0 = 12.0 Enter the operation For addition + For subtraction - For multiplication * For division / Any other key for exit : Output: The summation of 4.0 and 8.0 = 12.0
Related Programs
1) Simple Calculator Using If Else2) Program of Simple Calculator using switch
3) Program to calculate factorial using Recursion
4) Program to Check Leap Year
5) Program to find the average of numbers in a given range
6) Program to calculate Gross Salary
7) Program to find modulus of two numbers
8) Program to Display Fibonacci Series
9) Program to Display Fibonacci Series using Recursion
10) Program to find LCM(Least Common Multiple)
11) Program to Swap Two Numbers
12) Program to calculate power of a number
13) Program to Check Whether a Number is Prime or Not
14) Program to find HCF(Highest Common Factor)/GCD(Greatest Common Divisor) and LCM(Least Common Multiple)