27/01/2023

Write a C Program to Make a Simple Calculator Using Switch Case

Write a C Program to Make a Simple Calculator Using Switch Case or calculator program in C programming language. calculator program in c is very simple I am going to solve this problem by using a switch case and a while loop. So this problem has basically 3 steps or 3 conditions. The first condition step is to take input from the user and the second step is to perform the task and print the outcome in the console string the last step is to check if there is any wrong condition or wrong operator then it will show an error "Try again". Go through all three steps in the explanation section so you can understand better.

C Program to Make a Simple Calculator Using Switch Case

Calculator Program in C Explanation


So step one is to Take input from the user but before that use a "While Loop" and put the condition 1 or true, this will help you to run our program continually no need to run the program, again and again, which means while you are taking a number form user all operations will be performed in the same manner like in Real Calculator.

Step 1: Take Input From the User


printf("\nEnter First Value:");
scanf("%d",&num1);

printf("\nEnter Operator(+, -, *, /, %):");
//choice=getch();
scanf(" %c",&choice);

printf("\nEnter Second Value:");
scanf("%d",&num2);

See in the middle section of the code here we can use both comment line statement and scanf() statement, see care full in scanf() there is a difference. There is first blank space then %c is written.

Step 2- Arithmetic Operations


Perform all arithmetic operations of the calculator and print the output on the console screen like the below addition output is working.


result = num1 + num2;
printf("\nSum is = %d",result);

As we can see from the program if the user enters the operator then the same case perform and the output print the screen and break the operation avoid performing all operation in the switch case

Step 3: Using Default Switch Case


This is the last step for performing violate conditions in our programs like two operators with the same time and any other operator except the arithmetic operator for this we use a Default case. The default case shows the message that You have made a wrong decision.

H/W:- I recommended reading this full article, this will help you to build your own logic and solve the problem by using "Do While" and "For Loop".

Simple Calculator Using Switch Case in C


#include<stdio.h>
#include<conio.h>
main()
{
char choice;
int num1, num2, result = 0;

while(1)
{
printf("\nEnter First Value:");
scanf("%d",&num1);

printf("\nEnter Operator(+, -, *, /, %):");
//choice=getch();
scanf(" %c",&choice);

printf("\nEnter Second Value:");
scanf("%d",&num2);

switch(choice)
{
case '+':
   result = num1 + num2;
   printf("\nSum is = %d",result);
 break;
 
case '-':
   result = num1 - num2;
   printf("\nDifference is = %d",result);
   printf("\n\nPress Enter Again for New Input\n");
 break;
 
case '*':
   result = num1 * num2;
   printf("\nProduct is = %d",result);
   printf("\n\nPress Enter Again for New Input\n");
 break;
 
case '/':
   result = num1 / num2;
   printf("\nQuotient is = %d",result);
   printf("\n\nPress Enter Again for New Input\n");
 break;
 
case '%':
   result = num1 % num2;
   printf("\nReminder is = %d",result);
   printf("\n\nPress Enter Again for New Input\n");
 break; 
 
default:
   printf("\nEnter Valid Operator!!!\n");
   printf("\n\nPress Enter Again for New Input\n");
}
getch();
}
}

The Output C Program to Make a Simple Calculator Using Switch Case


Simple Calculator Using Switch Case in C

Similar to a Simple Calculator Using Switch Case


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: