In this Calculator program in C using a switch case, we will perform arithmetic operations (i.e., addition, subtraction, multiplication, and division) between two numbers in c language using a switch case and do-while loop.

## Calculator program in C using switch case

In this post, we will make a simple calculator using a switch case in c. We can add, subtract, multiply and divide two numbers using this program.

```#include <stdio.h>
int main()
{
int a, b, ch;
char choice;
printf("\n Enter two numbers:");
scanf("%d%d", &a, &b);
do
{
printf("\n Press 1 to add %d and %d", a, b);
printf("\n Press 2 to subtract %d and %d", a, b);
printf("\n Press 3 to multiply %d and %d", a, b);
printf("\n Press 4 to divide %d and %d", a, b);
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Sum: %d", a + b);
break;
case 2:
printf("Subtract :%d", a - b);
break;
case 3:
printf("Multiply :%d", a * b);
break;
case 4:
if (b == 0)
printf("\n Denominator cannot be zero");
else
printf("Divide :%d", a / b); // b should not be zero
break;
default:
printf("Wrong choice!");
}
printf("\n Do you want to continue? (Press y/n) ");
scanf(" %c", &choice);
} while (choice == 'y');
return 0;
}
```
You may want to read this post :

Output:

Enter two numbers:5 3

Press 1 to add 5 and 3
Press 2 to subtract 5 and 3
Press 3 to multiply 5 and 3
Press 4 to divide 5 and 3
Sum: 8
Do you want to continue? (Press y/n)n

### Approach to make a simple calculator program using switch case and do-while loop:-

1. Initialize three numbers, one number is for the user’s choice, and we perform arithmetic operations on the other two numbers.
2. Input two numbers on which we perform arithmetic operations.
3. Print all the options and input the preference of the user.
4. Using the switch keyword, we write all four cases for addition, subtraction, multiplication, and division.
5. Do not forget to write a break at the end of the case statement.
6. You can also write a default statement if the user inputs the wrong choice.
7. for example: – default:printf(“Wrong choice!”);

## Simple Calculator Program using functions

In this program, we are making a Calculator program in C using Switch Case and functions.

```#include <stdio.h>
{
return a+b;
}

int subtract(int a,int b)
{
return a-b;
}

int multilply(int a,int b)
{
return a*b;
}

int divide(int a,int b)
{
return a/b;
}
int main()
{
int a, b;
char choice;
printf("\n Enter two numbers:");
scanf("%d%d", &a, &b);

printf("\n Enter Your Choice (+,-,*,/): ");
scanf(" %c", &choice);
switch (choice)
{
case '+':

break;
case '-':
printf("Subtract :%d", subtract(a,b));
break;
case '*':
printf("Multiply :%d", multilply(a,b));
break;
case '/':
if (b == 0)
printf("\n Denominator cannot be zero");
else
printf("Divide :%d", divide(a,b)); // b should not be zero
break;
default:
printf("Wrong choice!");
}
return 0;
}

```

## Calculator in c using while loop

In this program, we will make a simple calculator using a while loop and an if-else statement instead of a switch case.

### Create a Calculator using while loop and if-else in c

```#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1, num2, choice;
while (1)
{
printf("\n Menu Driven Program in c");
printf("\n 2.Subtraction");
printf("\n 3.Multiplication");
printf("\n 4.Division");
printf("\n 5.exit");
printf("\n Enter Choice:");
scanf("%d", &choice);
if (choice > 0 && choice < 6)
{
if (choice == 1)
{
printf("\n Enter 1st number: ");
scanf("%d", &num1);
printf("\n Enter 2nd number: ");
scanf("%d", &num2);
printf("\n %d + %d = %d", num1, num2, num1 + num2);
}
if (choice == 2)
{
printf("\n Enter 1st number: ");
scanf("%d", &num1);
printf("\n Enter 2nd number: ");
scanf("%d", &num2);
printf("\n %d - %d = %d", num1, num2, num1 - num2);
}

if (choice == 3)
{
printf("\n Enter 1st number: ");
scanf("%d", &num1);
printf("\n Enter 2nd number: ");
scanf("%d", &num2);
printf("\n %d * %d = %d", num1, num2, num1 * num2);
}

if (choice == 4)
{
printf("\n Enter 1st number: ");
scanf("%d", &num1);
printf("\n Enter 2nd number: ");
scanf("%d", &num2);
if (num2 == 2)
printf("\n Denominator cannot be zero");
else
{
printf("\n %d / %d = ", num1, num2);
printf("%d", num1 / num2);
}
}
if (choice == 5)
exit(0);
}
else
printf("\n Wrong choice");
}
return 0;
}
```

#### Output for Menu-driven Calculator Program using while-loop and if-else

2. Subtraction
3. Multiplication
4. Division
5. exit
Enter Choice:1
Enter 1st number: 5
Enter 2nd number: 6
5 + 6 = 11

## C Program to make Calculator using Switch Case

Durgesh Kumar gives this code. He was facing a problem with the same program. He was not able to run his last line of code. See in the comment section of this post.

When taking a character input, you must write scanf(” %c” &choice);. You have to give a space between ” & %.

`#include <stdio.h>int main(){    int num1, num2, opt;    char choice;    // taking the input from the users    printf("enter the first integer= ");    scanf("%d", &num1);    printf("enter the second integer= ");    scanf("%d", &num2);    printf("*****************************");    do    {        printf("n please enter your choice. nn 1.Additionn 2.Subtraction n 3.Multiplication n 4.division n 5.Modulasn");        scanf("%d", &opt);        switch (opt)        {        case 1:            printf("The addition of %d + %d = %d", num1, num2, num1 + num2);            break;        case 2:            printf("the subtraction of %d - %d = %d", num1, num2, num1 - num2);            break;        case 3:            printf("The multiplication of %d * %d = %d", num1, num2, num1 * num2);            break;        case 4:            if (num2 == 0)            {                printf("this number can't divide by zeron");            }            else            {                printf("the division of %d / %d = %d", num1, num2, num1 / num2);            }            break;        case 5:            if (num2 == 0)            {                printf("we can't find the remainder");            }            else            {                printf("the remainder of %d % %d = %d", num1, num2, num1 % num2);            }            break;        default:            printf("Please choose the correct option");        }        printf("ndo you want to continue? press(y/n)");        scanf(" %c", &choice);    } while (choice == 'y');    return 0;}`