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.
Table of Contents
Calculator program in C using switch case
#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);
printf("\n Enter Your Choice: ");
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;
}
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
Enter Your Choice: 1
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:-
- Initialize three numbers, one number is for the user's choice, and we perform arithmetic operations on the other two numbers.
- Input two numbers on which we perform arithmetic operations.
- Print all the options and input the preference of the user.
- Using the switch keyword, we write all four cases for addition, subtraction, multiplication, and division.
- Do not forget to write a break at the end of the case statement.
- You can also write a default statement if the user inputs the wrong choice.
- for example: - default:printf("Wrong choice!");
Simple Calculator Program in C using functions
In this program, we are making a Calculator program in C using Switch Case and functions.
#include <stdio.h>
int add(int a,int b)
{
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 '+':
printf("Sum: %d", add(a,b));
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;
}
Program for 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.
C program to create a Calculator using if-else
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num1, num2, choice;
while(1)
{
printf("\n Menu Driven Program in c");
printf("\n 1.Addition");
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
1. Addition
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
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. \n\n 1.Addition\n 2.Subtraction \n 3.Multiplication \n 4.division \n 5.Modulas\n");
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 zero\n");
}
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;
}