# Simple Calculator Using Menu Driven Program in C

## Introduction

In this post, I am going to write a menu-driven program in c for a simple calculator. This is a very interesting program. In this program, we are going to create a menu using a switch case statement in c programming. So, before writing our program, let’s see the expected output first.

### Expected Output 1 Menu Driven Program in C

``````Select the Operation
Type - for Subtraction
Type * for Multiplication
Type / for Division
+
Enter any two numbers
5 10
5 + 10 = 15``````

### Expected Output 2 For the Same Program

``````Select the Operation
Type - for Subtraction
Type * for Multiplication
Type / for Division
/
Enter any two numbers
5 0
Sorry, You can not divide a number by 0``````

As you can see, there are two outputs. In the first output, we are performing the addition operation and in the second output, we are performing division operation. We must always remember that when we divide any number by 0 then the answer is infinity.

One more thing, this program will work only on integer numbers. There are chances that division operation will show the result in the real form. For this, I have used the concept type casting. Now, see the actual program.

### Simple Calculator Using Menu Driven Program in C

``````#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b;
char ch;
printf("Select the Operation\n");
printf("Type - for Subtraction\n");
printf("Type * for Multiplication\n");
printf("Type / for Division\n");
scanf("%c",&ch);
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
switch(ch)
{
case '+':
printf("%d + %d = %d",a,b,(a+b));
break;
case '-':
printf("%d - %d = %d",a,b,(a-b));
break;
case '*':
printf("%d * %d = %d",a,b,(a*b));
break;
case '/':
if(b==0)
{
printf("Sorry, You can not divide a number by 0");
return 0;
}
printf("%d / %d = %0.2f",a,b,(a/(float)b));
break;
default:
printf("Sorry, Invalid Choice");
}
return 0;
}
``````

In the above program, we have used switch case. Whenever, we have to use menu driven programs, then we can use switch case in c programming.

I hope you have liked this program. If you have any doubt or suggestions for me, then please feel free to contact me.

Thank you.