Introduction:
In this tutorial, we will learn how to create a simple calculator program in the C programming language. The calculator will be able to perform basic arithmetic operations like addition, subtraction, multiplication, and division. This project serves as a great way for beginners to practice control structures, functions, and handling user input in C.
Objective:
The objective of this project is to create a C program that asks the user for two numbers and an operation to perform (add, subtract, multiply, or divide), and then prints the result of that operation. The program will be interactive, allowing users to input their data and get instant results. Additionally, we will include error handling for invalid input and division by zero.
Code:
#include void add(float a, float b) { printf("Result: %.2f\n", a + b); } void subtract(float a, float b) { printf("Result: %.2f\n", a - b); } void multiply(float a, float b) { printf("Result: %.2f\n", a * b); } void divide(float a, float b) { if (b != 0) { printf("Result: %.2f\n", a / b); } else { printf("Error! Division by zero is not allowed.\n"); } } int main() { float num1, num2; char operator; // Ask for user input printf("Enter first number: "); scanf("%f", &num1); printf("Enter an operator (+, -, *, /): "); scanf(" %c", &operator); // The space before %c is to consume any leading whitespace characters printf("Enter second number: "); scanf("%f", &num2); // Perform the operation based on the operator entered switch (operator) { case '+': add(num1, num2); break; case '-': subtract(num1, num2); break; case '*': multiply(num1, num2); break; case '/': divide(num1, num2); break; default: printf("Error! Invalid operator.\n"); } return 0; }
Explanation of the Program Structure:
The program is structured in the following way:
- Functions for Operations:We define four functions for performing the basic arithmetic operations: add, subtract, multiply, and divide. Each function takes two float arguments (the numbers to operate on) and prints the result.
- Input Section:The program first asks the user to input two numbers and an operator. It uses
scanf
to get the input values from the user. The operator is captured as a character (‘+’, ‘-‘, ‘*’, or ‘/’). - Switch Statement for Operation:Based on the operator input by the user, a
switch
statement is used to choose the corresponding function to perform the operation. If the operator is invalid, an error message is displayed. - Error Handling:We have included error handling for division by zero. If the user attempts to divide by zero, the
divide
function will print an error message.
How to Run the Program:
Follow these steps to run the program:
-
- Step 1: Open your text editor or Integrated Development Environment (IDE) and create a new C file (e.g.,
calculator.c
). - Step 2: Copy the code provided above into your C file.
- Step 3: Save the file and open a terminal or command prompt in the directory where the file is saved.
- Step 4: Compile the C program by typing the following command (assuming you’re using GCC compiler):
- Step 1: Open your text editor or Integrated Development Environment (IDE) and create a new C file (e.g.,
gcc calculator.c -o calculator
-
- Step 5: Run the compiled program with the following command:
./calculator
- Step 6: The program will prompt you for input. Enter the numbers and operator as instructed to get the result.
Example Output:
Enter first number: 5 Enter an operator (+, -, *, /): + Enter second number: 3 Result: 8.00