Today we will learn how to make a Simple Calculator program in C and also learn Calculator program in C using switch case, functions and else if statements. So before starting, we will take an overview of the functioning of calculator.
Table of Contents
How Calculator Works?
After executing the program the compiler asks the user to enter the sign like ‘+’ for addition,’-‘ for subtraction,’*’ for multiplication,’/’ for the division. When the user input sign then using switch statement compiler finds for that case operation in the program.
When the compiler finds that case compiler starts executing and then ask the user to input two numbers so that the program can do operation on that two numbers.
And finally, compiler prints the output on the screen.
Calculator Program in C Algorithm
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.
#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; default: printf("\nError"); } }
Output:
In the above 1 output Addition operation is done because of user input 1 for addition. In second output multiplication is done because of input 3 for operation.
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.
#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; }
Output:
In the above program, the compiler is asking the user to enter the arithmetic operators such as +,-,*,/ and also ask to input two numbers so the operations can be done using two numbers.
3. Simple Calculator Program in C Using if-else Statement
In this program, we will use else if statement instead of switch case statement.
#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); if(Operator == '+') { printf("\n The result of %.2f + %.2f = %.2f", num1, num2, num1 + num2); } else if(Operator == '-') { printf("\n The result of %.2f - %.2f = %.2f", num1, num2, num1 - num2); } else if(Operator == '*') { printf("\n The result of %.2f * %.2f = %.2f", num1, num2, num1 * num2); } else if(Operator == '/') { printf("\n The result of %.2f / %.2f = %.2f", num1, num2, num1 / num2); } else { printf("\n You have enetered an Invalid Operator "); } return 0; }
Output:
4. Calculator Program in C Using Function
In this program, we have declared user-defined functions.
#include<stdio.h> #include<stdlib.h> //function declarations void display(float n1, float n2, char ch, float result); void add(float n1, float n2); void subtract(float n1, float n2); void multiply(float n1, float n2); void divide(float n1, float n2); void rem(float n1, float n2); void power(float n1, float n2); //main function int main() { float n1, n2; int ch; do{ printf("Enter two numbers: "); scanf("%f %f", &n1, &n2); printf("\n*****************"); printf("\n1.Addition"); printf("\n2.Subtraction"); printf("\n3.Multiplication"); printf("\n4.Division"); printf("\n5.Remainder"); printf("\n6.Power (x^y)"); printf("\n7.Exit"); printf("\nEnter your choice: "); scanf("%d", &ch); switch (ch) { case 1: add(n1,n2); break; case 2: subtract(n1,n2); break; case 3: multiply(n1,n2); break; case 4: divide(n1,n2); break; case 5: rem(n1,n2); break; case 6: power(n1,n2); break; case 7: printf("Thank You."); exit(0); default: printf("Invalid input."); printf("Please enter correct input."); } printf("\n**********************************\n"); }while(1); return 0; } //function for displaying the result void display(float n1, float n2, char ch, float result) { printf("%.2f %c %.2f = %.2f\n", n1, ch, n2, result); } //function for addition of two numbers void add(float n1, float n2) { float result = n1 + n2; display(n1, n2, '+', result); } //function for subtraction of two numbers void subtract(float n1, float n2) { float result = n1 - n2; display(n1, n2, '-', result); } //function for multiplication of two numbers void multiply(float n1, float n2) { float result = n1 * n2; display(n1, n2, '*', result); } //function for division of two numbers void divide(float n1, float n2) { float result = n1 / n2; display(n1, n2, '/', result); } //function for calculating remainder void rem(float n1, float n2) { //Modulus operator only works on int data type //Floating numbers are converted to int number int num1 = n1; int num2 = n2; int result = num1%num2; printf("%d %% %d = %d\n", num1, num2, result); } //function for calculating power void power(float n1, float n2) { if(n2<0) printf("Second number should be +ve."); else { float result=1.0; for(int i=1; i<=n2; i++) { result *= n1; } display(n1, n2, '^', result); } }
Also Read:
- C Program to print Prime Numbers from 1 to n
- Armstrong Number Program in C
- Fibonacci Series Program in C
- Decimal to Binary Conversion Program in C
- Reverse a String in C
- Program to Reverse a Number in C
- Hello World Program in C
- Palindrome Program in C
- Leap Year Program in C
- Factorial Program in C
- Prime Number Program in C