In this article, we will learn how to write a calculator program in c. We will write a simple calculator program in c language to perform basic arithmetic operations like addition, subtraction, multiplication, division, modulus and etc. A calculator is generally used to perform complex arithmetic tasks like logarithmic functions, trigonometric functions, degrees, radians, hyperbolic functions, and many more. Let’s see how to write an algorithm for a simple calculator program in c.
Algorithm for a calculator program in c:
Step 1: Declare variables number1, number2, answer, and operation. Number1 and number2 are to take two operands, the answer is to store the result of the operation.
Step 2: A print statement for taking two numbers from the user.
Step 3: Take two inputs from the user, number1 and number 2
Step 4: Print a statement to give options to select operators (addition, subtraction, division, multiplication, etc.)
Step 5: Take the operator choice from the user.
Step 6: Calculate the result and store it in the answer.
Step 7: Display the answer.
Step 8: Close the program.
We have various approaches to writing a simple calculator program in c:
 The simple calculator program in c using the ifelse statement.
 The simple calculator program in c using a switch case
 The simple calculator program in c using function and switch case
Approach 1: The simple calculator program in c using the ifelse statement.
In this approach, we will use ifelse statements to operate. Let’s see how to write code for a calculator program in c.
Program Code in C using ifelse
#include <stdio.h> int main() { int number1, number2; float answer; char op; printf (" Enter the operation to perform(+, , *, /) \n "); scanf ("%c", &op); printf (" Enter the first number: "); scanf(" %d", &number1); printf (" Enter the second number: "); scanf (" %d", &number2); //addition if (op == '+') { answer = number1 + number2; printf (" %d + %d = %f", number1, number2, answer); } //substraction else if (op == '') { answer = number1  number2; printf (" %d  %d = %f", number1, number2, answer); } //multiplication else if (op == '*') { answer = number1 * number2; printf (" %d * %d = %f", number1, number2, answer); } //division else if (op == '/') { if (number2 == 0) { printf (" \n Divisor cannot be zero. Please enter another value "); scanf ("%d", &number2); } answer = number1 / number2; printf (" %d / %d = %.2f", number1, number2, answer); } else { printf(" \n Enter valid operator "); } return 0; }
Output
Enter the operation to perform(+, , *, /)
*
Enter the first number: 2
Enter the second number: 6
2 * 6 = 12.000000
In the above program, first, we declared variables number1, number2, answer, and op. After that, we take input from the user for number1, number2, and op. Now, we will check for the input operator, if the given operator is + then we will perform an addition and print the answer. Similarly, we will check the given operator is .* or / and we will perform subtraction, multiplication, and division accordingly. We will check another condition in a division that number 2 is not zero because we can not divide any number by zero. If the given operator is not among the given operators we will inform the user to enter a valid operator. In the output, we can see that first, we take the operator input which is multiplication after that, we took two numbers 2 and 6, and then we perform multiplication of the two numbers and print the answer.
Approach 2: The simple calculator program in c using the switch case.
In this approach, we will use a switch case to operate. Let’s see how to write code for a calculator program in c.
Calculator Program Using Switch
#include <stdio.h> int main() { int number1, number2; float answer; char op; printf (" Enter the operation to perform(+, , *, /) \n "); scanf ("%c", &op); printf (" Enter the first number: "); scanf(" %d", &number1); printf (" Enter the second number: "); scanf (" %d", &number2); switch(op){ //addition case '+': answer = number1 + number2; printf (" %d + %d = %f", number1, number2, answer); break; //substraction case '': answer = number1  number2; printf (" %d  %d = %f", number1, number2, answer); break; //multiplication case '*': answer = number1 * number2; printf (" %d * %d = %f", number1, number2, answer); break; //division case '/': if (number2 == 0) { printf (" \n Divisor cannot be zero. Please enter another value "); scanf ("%d", &number2); } answer = number1 / number2; printf (" %d / %d = %.2f", number1, number2, answer); break; default: printf(" \n Enter valid operator "); } return 0; }
Output
Enter the operation to perform(+, , *, /)

Enter the first number: 23
Enter the second number: 11
23  11 = 12.000000
In the above program, first, we declared variables number1, number2, answer, and op. After that, we take input from the user for number1, number2, and op. Now, we will check for the input operation using the switch statement, if the given operator is + then we will perform an addition and print the answer. Similarly, we will check the given operator is .* or / and we will perform subtraction, multiplication, and division accordingly. We will check another condition in a division that number 2 is not zero because we can not divide any number by zero. If the given operator is not among the given operators we will use the default statement to inform the user to enter a valid operator. In the output, we can see that first, we take the operator input which is subtraction after that, we took two numbers 23 and 11, and then we perform subtraction of the two numbers and print the answer 12.
Approach 3: The simple calculator program in c using the functions and switch case.
In this approach, we will create functions to operate. Let’s see how to write code for a calculator program in c.
Simple Calculator Program in C Using Functions
#include <stdio.h> #include <stdlib.h> int addition(); int subtraction(); int multiplication(); int division(); int main() { char op; printf (" Enter the operation to perform(+, , *, /) \n "); scanf ("%c", &op); switch (op) { case '+': addition(); break; case '': subtraction(); break; case '*': multiplication(); break; case '/': division(); break; default: printf("\n enter valid operator"); break; } return 0; } int addition() { int number1, number2, answer; printf (" Enter the first number: "); scanf (" %d", &number1); printf (" Enter the second number: "); scanf (" %d", &number2); answer = number1 + number2; printf (" %d + %d = %d", number1, number2, answer); return 0; } int subtraction() { int number1, number2, answer; printf (" Enter the first number: "); scanf (" %d", &number1); printf (" Enter the second number: "); scanf (" %d", &number2); answer = number1  number2; printf (" %d  %d = %d", number1, number2, answer); } int multiplication() { int number1, number2, answer; printf (" Enter the first number: "); scanf (" %d", &number1); printf (" Enter the second number: "); scanf (" %d", &number2); answer = number1 * number2; printf (" %d * %d = %d", number1, number2, answer); } int division() { int number1, number2, answer; printf (" Enter the first number: "); scanf (" %d", &number1); printf (" Enter the second number: "); scanf (" %d", &number2); if (number2 == 0) { printf (" \n Divisor cannot be zero. Please enter another value "); scanf ("%d", &number2); } answer = number1 / number2; printf (" \n %d / %d = %d", number1, number2, answer); }
Output
Enter the operation to perform(+, , *, /)
*
Enter the first number: 3
Enter the second number: 4
3 * 4 = 12
In the above program, first, we have created four function addition, subtraction, multiplication, and division. After that, we took input from the user to perform the operation accordingly. Now, we will check for the input operation using the switch statement, if the given operator is + then we will perform an addition and print the answer. Similarly, we will check the given operator is .* or / and we will perform subtraction, multiplication, and division accordingly. We will check another condition in a division that number 2 is not zero because we can not divide any number by zero. If the given operator is not among the given operators we will use the default statement to inform the user to enter a valid operator. In the output, we can see that first, we take the operator input which is multiplication after that, we took two numbers 3 and 4, and then we perform multiplication of the two numbers and print the answer 12.
Other C Programs
C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Ascending Order Program in C
Menu Driven Program For All Operations On Doubly Linked List in C
C Program for Armstrong Number
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Perfect Number Program in C
Leap Year Program in C
Odd Even Program in C
Selection Sort Program in C
Linear Search Program in C
While Loop Program in C
C Program to Swap Two Numbers