A Menu-Driven Program to perform a simple calculation.

Here, we have a basic program example to perform simple arithmetic calculation driven by menu using different languages. This program is created in c language, c++, Java, and Python.

Code to perform simple calculation in C language

#include <stdio.h>
void main ()
{
      int opt, num1, num2;
      printf("Enter the first Integer : ");
      scanf("%d",&num1);
      printf("Enter the second Integer : ");
      scanf("%d",&num2);

      printf("\nInput your option :\n");
      printf("1-Addition \n");
      printf("2-Substraction \n");
      printf("3-Multiplication \n");
      printf("4-Division \n");
      printf("5-Exit \n \n");
      scanf("%d",&opt);

      switch(opt) {
      case 1:
        printf("The Addition of  %d and %d is: %d\n",num1,num2,num1+num2);
        break;

      case 2:
        printf("The Substraction of %d  and %d is: %d\n",num1,num2,num1-num2);
        break;

      case 3:
        printf("The Multiplication of %d  and %d is: %d\n",num1,num2,num1*num2);
        break;

      case 4:
        if(num2==0) {
          printf("The second integer cannot be zero. \n");
        } else {
          printf("The Division of %d  and %d is : %d\n",num1,num2,num1/num2);
        }
        break;

      case 5:
        break;

      default:
        printf("Input correct option\n");
        break;
    }
}

Code to perform simple calculation in C++ language

#include<iostream>
using namespace std;
int main()
{
      int opt, num1, num2;
      cout<<"Enter the first Integer : ";
      cin>>num1;
      cout<<"Enter the second Integer : ";
      cin>>num2;

      cout<<"\nInput your option :\n";
      cout<<"1-Addition \n";
      cout<<"2-Substraction \n";
      cout<<"3-Multiplication \n";
      cout<<"4-Division \n";
      cout<<"5-Exit \n \n";
      cin>>opt;

      switch(opt) {
      case 1:
        cout<<"The Addition of "<<num1<<" and "<<num2<<" is: "<<num1+num2;
        break;

      case 2:
        cout<<"The Substraction of "<<num1<<" and "<<num2<<" is: "<<num1-num2;
        break;

      case 3:
        cout<<"The Multiplication of "<<num1<<" and "<<num2<<" is: "<<num1*num2;
        break;

      case 4:
        if(num2==0){
          cout<<"The second integer cannot be zero. \n";
        } else {
          cout<<"The Division of "<<num1<<" and "<<num2<<" is: "<<num1/num2;
        }
        break;

      case 5:
        break;

      default:
        cout<<"Input correct option\n";
        break;
    }
}

Code to perform simple calculation in Python language

num1 = int(input("Enter the first Integer : "))
num2 = int(input("Enter the second Integer : "))

print("1-Addition")
print("2-Substraction")
print("3-Multiplication")
print("4-Division")

opt = int(input("Input yout option: "))

if(opt==1):
    print("The Addition of ",num1, "and", num2, "is: ", num1+num2)
elif(opt==2):
    print("The Substraction of ",num1, "and", num2, "is: ", num1-num2)
elif(opt==3):
    print("The Multiplication of ",num1, "and", num2, "is: ", num1*num2)
elif(opt==4):
    if(num2==0):
        print("The second number cannot be a zero.")
    else:
        print("The Addition of ",num1, "and", num2, "is: ", num1/num2)
else:
    print("Input correct options.")

Code to perform simple calculation in Java language

import java.util.*;
public class calc {

    public static void main(String[] args) {
      
      Scanner sc=new Scanner(System.in);

      int opt, num1, num2;
      System.out.println("Enter the first Integer : ");
      num1 = sc.nextInt();
      System.out.println("Enter the second Integer : ");
      num2 = sc.nextInt();
      
      System.out.println("Input your option :");
      System.out.println("1-Addition ");
      System.out.println("2-Substraction ");
      System.out.println("3-Multiplication ");
      System.out.println("4-Division ");
      System.out.println("5-Exit");
      
      opt = sc.nextInt();

      switch(opt) {
      case 1:
        int sum = num1 + num2;
        System.out.println("The Addition of "+ num1 + " and " + num2 + " is: " +sum);
        break;

      case 2:
        int sub = num1 - num2;
        System.out.println("The Addition of "+ num1 + " and " + num2 + " is: " +sub);
        break;

      case 3:
        int mul = num1 * num2;
        System.out.println("The Addition of "+ num1 + " and " + num2 + " is: " +mul);
        break;

      case 4:
        if(num2==0) {
          System.out.println("The second integer cannot be zero. ");
        } else {
                 int div = num1 / num2;
                 System.out.println("The Addition of "+ num1 + " and " + num2 + " is: " +div);
      
        }
        break;

      case 5:
        break;

      default:
        System.out.println("Input correct option");
        break;
    }   
    }
}