Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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

C Conditional Statement: Exercise-26 with Solution

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

Pictorial Presentation:

A menu-driven program for a simple calculator

Sample Solution:

C Code:

#include <stdio.h>
void main() {
  int num1,num2,opt;
  printf("Enter the first Integer :");
  scanf("%d",&num1);
  printf("Enter the second Integer :");
  scanf("%d",&num2);
  
    printf("\nInput your option :\n");
    printf("1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-Exit.\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 is zero. Devide by 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; 
    }
}

Sample 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

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C which is a Menu-Driven Program to compute the area of the various geometrical shape.
Next: C For Loop Exercises Home

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP