Thursday, 10 March 2016

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.

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


If you have any query feel free to share with me, and if you like my work please share, comments and subscribe for our new and latest post related to programming this will help you update all about a new post.

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


calculator program in c


You May Also Like:-

1. Java Program For Calculator Using AWT Controls (GUI)

2. Creating A Simple Calculator with jQuery With CSS

3. Build A Simple Calculator Using HTML Form Elements And JavaScript.

4. C Program For Find A Grade Of Given Marks Using Switch Case

5. C++ Program For Store Employee Information And Display Using Structure

6. C Program For Finding Radius Circumference Using Switch Case

7. Java Program For Find The Gross Salary Of An Employee

8. Hacker Rank Solution For Birthday Cake Candles

9. C++ Program For Calculate Percentage Of 5 Subjects

10. Hacker Rank solution for 30 Days Of Code



No comments:
Write comments

Recommended Posts × +