This example program accepts two input numbers and performs an arithmetic operation such as addition, subtraction, division, multiplication and mod operation on them. The output is printed to the console.
Program Definition
This calculator program is built using C arithmetic operators. The user is presented with a list of choices. Once the user input his or her choice that operation is performed.
Here is the list of operations presented to the users.
- Addition
- Subtraction
- Multiplication
- Division
- Modulo
- Close
The C calculator is implemented using the switch-case mechanism of C language.
Calculator Program in C Algorithm (Pseudocode)
- 1 Step: BEGIN.
- 2 Step: PRINT ENTER YOUR CHOICE.
- 3 Step: ENTER YOUR CHOICE.
- 4 Step: ENTER TWO OPERANDS FOR OPERATION.
- 5 Step: USER WILL ENTER +,-,*,/ .
- 6 Step: SWITCH(OPERATOR)
- 7 Step: DO THE OPERATION.
- 8 Step: PRINT THE RESULT.
- 8 Step: EXIT.
There are different methods to write a Calculator program in C we will see those program’s one by one.
1. Calculator Program in C Using Switch Case
In this program, we will ask the user to input the operation sign and the program will start doing operation and will print the output on the screen.
Output:
Code:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#include<stdio.h> int main() { int choice; long num1, num2, x; printf("Please choose your option:" "\n1 = Addition" "\n2 = Subtraction" "\n3 = Multiplication" "\n4 = Division" "\n5 = Squares" "\n6 = exit" "\n\nChoice: "); scanf("%d", &choice); //while loop check whether the choice is in the given range while(choice < 1 || choice > 6) { printf("\nPlease choose the above mentioned option." "\nChoice: "); scanf("%d", &choice); } switch (choice) { case 1: printf("Enter two numbers: \n"); scanf("%ld %ld", &num1, &num2); x = num1 + num2; printf("Sum = %ld", x); break; case 2: printf("Enter two numbers: \n"); scanf("%ld %ld", &num1, &num2); x = num1 - num2; printf("Subtraction = %ld", x); break; case 3: printf("Enter two numbers: \n"); scanf("%ld %ld", &num1, &num2); x = num1 * num2; printf("Product = %ld", x); break; case 4: printf("Enter Dividend: "); scanf("%d", &num1); printf("Enter Divisor: "); scanf("%d", &num2); //while loop checks for divisor whether it is zero or not while(num2 == 0) { printf("\nDivisor cannot be zero." "\nEnter divisor once again: "); scanf("%d", &num2); } x = num1 / num2; printf("\nQuotient = %ld", x); break; case 5: printf("Enter any number: \n"); scanf("%ld", &num1); x = num1 * num1; printf("Square = %ld", x); break; case 6: return 0; default: printf("\nError"); } } |
2. Directly doing Operation in Case Statement
This program is the same as the above program but we will do operation directly in the case statement. But, the logic behind the program is the same.
Output:
Code:
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 |
#include <stdio.h> int main() { char Operator; float num1, num2, result = 0; printf("\n Please Enter an Operator (+, -, *, /) : "); scanf("%c", &Operator); printf("\n Please Enter the Values for two Operands: num1 and num2 : "); scanf("%f%f", &num1, &num2); switch(Operator) { case '+': printf("\n The result of %.2f + %.2f = %.2f", num1, num2, num1 + num2); break; case '-': printf("\n The result of %.2f - %.2f = %.2f", num1, num2, num1 - num2); break; case '*': printf("\n The result of %.2f * %.2f = %.2f", num1, num2, num1 * num2); break; case '/': printf("\n The result of %.2f / %.2f = %.2f", num1, num2, num1 / num2); break; default: printf("\n You have enetered an Invalid Operator "); } return 0; } |