C Program For Calculator Using Switch Case

Problem:- calculator program in c or simple calculator program in c using a switch or simple calculator program in c using do while or simple calculator program in c using while or C Program For Calculator Using Switch Case or C Program For Calculator Using While Loop.

Check This- Hacker rank Solution for Sub Domain Introduction, Classes, STL, Inheritance.

Logic:- calculator program in c is very simple of I am going to solve this problem by using switch case and while loop. So this problem has basically 3 step or 3 conditions, So the first condition of step is to take an input from user and second step is is to perform task and print the outcome in the console string and 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 step in explanation section you can understand better.

Explanation:- So step one is to Taking input from 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 that means while you are taking a number form user all operations will be performed in the same manner like in Real Calculator.

Step 1:-Taking an input from 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 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:- Perform all arithmetic operations of the calculator and print the output on the console screen like below addition output is working.

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

as we can see the program if user enter the operator than the same case perform and output print the screen and break operation avoid to perform all operation in switch case

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

Also check- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard Domain.

Solution:-

#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();
}
}

Output:-