Q:

C Program For Calculator Using Switch Case

0

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.

 

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

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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:

Enter First Value:1000

Enter Operator(+, -, *, /, %):+

Enter Second Value:2000-

Sum is = 3000

Enter First Value:2000

Enter Operator(+, -, *, /, %):-

Enter Second Value:1000

Difference is = 1000

Press Enter Again for New Input

Enter First Value:50

Enter Operator(+, -, *, /, %):*&*

Enter Second Value:50

Product is = 2500

Press Enter Again for New Input

Enter First Value:

Enter First Value:1000

Enter Operator(+, -, *, /, %):+

Enter Second Value:2000-

Sum is = 3000

Enter First Value:2000

Enter Operator(+, -, *, /, %):-

Enter Second Value:1000

Difference is = 1000

Press Enter Again for New Input

Enter First Value:50

Enter Operator(+, -, *, /, %):*&*

Enter Second Value:50

Product is = 2500

Press Enter Again for New Input

Enter First Value:

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now