Last Updated on May 25, 2023 by Prepbytes
In the realm of programming, calculators serve as indispensable tools for performing mathematical calculations efficiently. Building your own calculator program not only enhances your understanding of fundamental programming concepts but also provides you with a practical application to solve mathematical problems.
In this article, we will explore the process of creating a calculator program in the C programming language. We will take you through the stepbystep development of a calculator that can perform basic arithmetic operations, including addition, subtraction, multiplication, and division. Additionally, we will expand the functionality by incorporating more advanced operations, such as exponentiation and square root calculations.
Throughout the article, we will provide clear explanations and code examples, making it accessible for both beginners and experienced programmers. By following along, you will gain a deeper understanding of core programming concepts, such as user input, control flow, and function implementation.
Algorithm for a Calculator Program in C
 Step 1: Declare the following variables: 1, 2, answer, and operation. Numbers 1 and 2 need two operands, and the answer is where you store the outcome of the operation.
 Step 2: A print statement that asks the user for two integers.
 Step 3: Request the user’s input by asking for numbers 1 and 2.
 Step 4: Print a statement with options for addition, subtraction, division, multiplication, and other operators.
 Step 5: Take the user’s operator preference.
 Step 6: Calculate the outcome and enter it in the answer.
 Step 7: Show the solution.
 Step 8: Exit the application.
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 program, the user is prompted to enter an operator (+, , *, or /) and two numbers. The ifelse statements are used to determine the chosen operator and perform the corresponding arithmetic operation. If the division operator is chosen, the program checks for division by zero to prevent an error.
Finally, the result of the calculation is displayed on the screen. The program handles invalid operators gracefully and terminates execution if an error occurs. 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
First, we declared the variables number1, number2, answer, and op in the aforementioned program. The user is then asked to provide input for numbers 1, 2, and op. Now that we have checked the input operator, if it is +, we will add the numbers and report the result. Subtraction, multiplication, and division will be carried out in accordance with whether the provided operator is .* or /. Since we cannot divide any integer by zero, we shall verify additional conditions in a division that number 2 is not zero. We will prompt the user to enter a valid operator if the requested operator is not one of the available operators. We can see from the result that we started by taking the operator input, which is multiplication. In the following step, we took the numbers 2 and 6, multiplied them, and then printed the result.
Approach 2: The Simple Calculator Program in C using the Switch Case.
In this program, the user is prompted to enter an operator (+, , *, or /) and two numbers. The switch case statement is used to determine the chosen operator and perform the corresponding arithmetic operation. Similar to the ifelse version, the program checks for division by zero when the division operator is chosen.
Finally, the result of the calculation is displayed on the screen. The program handles invalid operators gracefully and terminates execution if an error occurs. 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
First, we declared the variables number1, number2, answer, and op in the aforementioned program. The user is then asked to provide input for numbers 1, 2, and op. Now, we will use the switch statement to check for the input operation; if the supplied operator is +, we will add the given values and output the result. Subtraction, multiplication, and division will be carried out in accordance with whether the provided operator is .* or /. Since we cannot divide any integer by zero, we shall verify additional conditions in a division that number 2 is not zero.We will utilize the default statement to prompt the user to provide a valid operator if the given operator is not one of the available operators. We can see from the output that we first enter the subtraction operator, then we take the two values 23 and 11, and last we subtract the two numbers, printing the result as 12.
Approach 3: The Simple Calculator Program in C using the Functions and Switch Case.
In this program, four separate functions (add, subtract, multiply, and divide) are defined to perform the corresponding arithmetic operations. The main function prompts the user to enter an operator (+, , *, or /) and two numbers. The switch case statement is then used to determine the chosen operator and call the appropriate function.
The result of the calculation is stored in the result variable and displayed on the screen. The program handles invalid operators gracefully and terminates execution if an error occurs during division by zero.
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
First, we constructed four functions for addition, subtraction, multiplication, and division in the aforementioned programme. We then used the user’s input to complete the action as needed. Now, we will use the switch statement to check for the input operation; if the supplied operator is +, we will add the given values and output the result. Subtraction, multiplication, and division will be carried out in accordance with whether the provided operator is .* or /. Since we cannot divide any integer by zero, we shall verify additional conditions in a division that number 2 is not zero. We will utilize the default statement to prompt the user to provide a valid operator if the given operator is not one of the available operators. We can see from the output that we first enter the multiplication operator, then we take the two values 3 and 4, and last we multiply the two numbers, printing the result as 12.
Conclusion
By customizing and expanding the calculator program, readers have the opportunity to explore further and adapt it to their specific needs and preferences. Whether you are a beginner learning the basics of C programming or an experienced developer honing your skills, the process of building a calculator program provides valuable insights and practical experience.
As you continue your programming journey, the knowledge and techniques gained from building a calculator program can be applied to a wide range of programming projects, from mathematical simulations to financial applications.
In conclusion, the calculator program in C serves as an excellent exercise to sharpen your coding abilities and solidify your understanding of programming fundamentals. Embrace the versatility and power of the C language, and let your imagination and creativity guide you in crafting more sophisticated and functional programs.
FAQ related to Calculator Program in C Language
Q1: What is a calculator program in C?
Ans. A calculator program in C is a software application that allows users to perform basic arithmetic calculations, such as addition, subtraction, multiplication, and division, using the C programming language.
Q2: What are the fundamental concepts involved in building a calculator program in C?
Ans. Building a calculator program in C involves understanding concepts such as user input, control flow (ifelse or switch case statements), arithmetic operations, and function implementation.
Q3: Can a calculator program in C handle more complex mathematical operations?
Ans. Yes, a calculator program in C can be expanded to handle more complex mathematical operations. This can include implementing scientific functions (e.g., square root, exponentiation, trigonometric functions), working with decimal numbers, or supporting memory functions (e.g., memory recall, memory store).
Q4: How can I handle errors, such as division by zero, in a calculator program?
Ans. To handle errors like division by zero in a calculator program, you can incorporate conditional statements or errorchecking mechanisms. Before performing division, you can check if the divisor is zero and display an appropriate error message to the user.
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