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.
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.
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 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.
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
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".
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
Similar to a Simple Calculator Using Switch Case
- Java Program For Calculator Using AWT Controls (GUI)
- Creating A Simple Calculator with jQuery With CSS
- Build A Simple Calculator Using HTML Form Elements And JavaScript.
- C Program For Find A Grade Of Given Marks Using Switch Case
- C++ Program For Store Employee Information And Display Using Structure
- C Program For Finding Radius Circumference Using Switch Case
- Java Program For Find The Gross Salary Of An Employee
- Hacker Rank Solution For Birthday Cake Candles
- C++ Program For Calculate Percentage Of 5 Subjects
- Hacker Rank solution for 30 Days Of Code
0 Comments: