Calculator Program in C
Here you get the example code of Calculator Program in C using Switch Case.
Calculator definition: An electronics machine, that is used to do the arithmetical operations on the numbers is called calculator. It may be simple calculator(used for simple calculations) of scientific calculator( used for big and advanced scientific calculations).
How to make Calculator?
Here we are making a simple Calculator program in c using switch case. In this program we will take two values a, b from the user, Then ask the user to enter a choice If he wants to perform Addition, Subtraction, Multiplication, or Division. After that, we will perform the operation choose by the user and display the result as output after execution.
Then we will perform Addition, Subtraction, Multiplication, and Division the operations according to the user’s choice the program asks the user for continuing the above process if the user enters “y” then all the above process starts again, if the user enters “n” then it will end the program.
Algorithm
Step 1: Input any two numbers.
Step 2: Display 1 for addition, 2 for subtraction, 3 for multiplication, 4 for division
Step 3: If the user inputs 1
           a. Add the both numbers and store in c.
           b. Print the value of c.
           c. Come out of the program.
Step 4: If the user inputs 2
         a. Calculate the difference of the both numbers and store in c.
          b. Print the value of c.
          c. Come out of the program.
Step 5: If the user inputs 3
         a. Multiply the both numbers and store in c.
          b. Print the value of c.
          c. Come out of the program.
Step 6: If the user inputs 4
         a. Divide the both numbers and store in c.
        b. Print the value of c.
         c. Come out of the program.
Step 7: Print Invalid choice.
Example of Calculator Program in C using Switch Case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | //calculator program in c #include<stdio.h> #include<conio.h> void main() { int a,b,c,choice; clrscr(); printf("\nEnter two values:"); scanf("%d%d",&a,&b); printf("\nOperations:\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division"); printf("\nEnter your choice:"); scanf("%d",&choice); switch(choice) { case 1: c=a+b; printf("\nResult of addition is :%d",c); break; case 2: c=a-b; printf("\nResult of subtraction is :%d",c); break; case 3 :c=a*b; printf("\nResult of multiplication is :%d",c); break; case 4: c=(float)(a)/b; printf("\nResult of division is :%d",c); break; default : printf("\nInvalid choice"); break; } getch(); } |
Input
Enter two values : 2Â 5
Operations:
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice : 1
Output
Result of addition is : 7
Check out our other C programming Examples
Very helping program code, easy to understand 🙂