w3resource

C Exercises: A menu-driven program for a simple calculator


26. Simple Calculation Menu

Write a program in C which is a Menu-Driven Program to perform a simple calculation.

Visual Presentation:

A menu-driven program for a simple calculator


Sample Solution:

C Code:

#include <stdio.h>  // Include the standard input/output header file.

void main() {
  int num1,num2,opt;  // Declare variables to store user input and operation choice.

  // Prompt user for two integers and store them.
  printf("Enter the first Integer :");
  scanf("%d",&num1);
  printf("Enter the second Integer :");
  scanf("%d",&num2);

  // Display the menu for operation choice.
  printf("\nInput your option :\n");
  printf("1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-Exit.\n");
  scanf("%d",&opt);  // Read and store the user's choice.

  switch(opt) {  // Start a switch statement based on the user's choice.
    case 1:
      printf("The Addition of  %d and %d is: %d\n",num1,num2,num1+num2);  // Perform addition and print result.
      break;

    case 2:
      printf("The Substraction of %d  and %d is: %d\n",num1,num2,num1-num2);  // Perform subtraction and print result.
      break;

    case 3:
      printf("The Multiplication of %d  and %d is: %d\n",num1,num2,num1*num2);  // Perform multiplication and print result.
      break;  

    case 4:
      if(num2==0) {
        printf("The second integer is zero. Divide by zero.\n");  // Handle division by zero.
      } else {
        printf("The Division of %d  and %d is : %d\n",num1,num2,num1/num2);  // Perform division and print result.
      }  
      break;

    case 5: 
      break;  // Exit the program.

    default:
      printf("Input correct option\n");  // Display error message for invalid input.
      break; 
  }
}

Output:

Enter the first Integer :10                                                                                   
Enter the second Integer :2                                                                                   
                                                                                                              
Input your option :                                                                                           
1-Addition.                                                                                                   
2-Substraction.                                                                                               
3-Multiplication.                                                                                             
4-Division.                                                                                                   
5-Exit.                                                                                                       
3                                                                                                             
The Multiplication of 10  and 2 is: 20 

Flowchart:

Flowchart: A menu driven program for a simple calculator


For more Practice: Solve these Related Problems:

  • Write a C program that provides a menu for addition, subtraction, multiplication, and division and loops until exit.
  • Write a C program to implement a menu-driven calculator using switch-case and function pointers.
  • Write a C program to perform simple calculations based on a menu selection and include error handling for division by zero.
  • Write a C program to build a menu-driven program that accepts two numbers, performs all basic operations, and logs each operation to a file.

Go to:


PREV : Geometrical Shapes Area Calculator.
NEXT : C For Loop Exercises Home

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.