Introduction
A basic calculator program allows users to perform simple arithmetic operations like addition, subtraction, multiplication, and division. The program typically prompts the user to input two numbers and an operator, then performs the corresponding operation and displays the result.
Example:
- Input:
- First number:
10
- Operator:
+
- Second number:
5
- First number:
- Output:
10 + 5 = 15
Problem Statement
Create a C program that:
- Prompts the user to input two numbers and an arithmetic operator.
- Performs the corresponding arithmetic operation based on the input operator.
- Outputs the result of the operation.
Solution Steps
- Include the Standard Libraries: Use
#include <stdio.h>
for standard input-output functions. - Implement the Basic Calculator Logic:
- Input two numbers and an operator from the user.
- Use a switch-case statement to determine the operation to perform.
- Display the result of the operation.
- Create a Main Function: Handle user inputs and call the appropriate logic to perform the calculation.
C Program to Implement a Basic Calculator
#include <stdio.h> int main() { char operator; double num1, num2, result; // Input the operator from the user printf("Enter an operator (+, -, *, /): "); scanf("%c", &operator); // Input the two numbers from the user printf("Enter two operands: "); scanf("%lf %lf", &num1, &num2); // Perform the calculation based on the operator switch (operator) { case '+': result = num1 + num2; printf("%.2lf + %.2lf = %.2lf\n", num1, num2, result); break; case '-': result = num1 - num2; printf("%.2lf - %.2lf = %.2lf\n", num1, num2, result); break; case '*': result = num1 * num2; printf("%.2lf * %.2lf = %.2lf\n", num1, num2, result); break; case '/': if (num2 != 0) { result = num1 / num2; printf("%.2lf / %.2lf = %.2lf\n", num1, num2, result); } else { printf("Error! Division by zero is not allowed.\n"); } break; default: printf("Error! Operator is not correct.\n"); break; } return 0; // Return 0 to indicate successful execution }
Explanation
Input Handling
- Operator: The program first prompts the user to input an operator (
+
,-
,*
, or/
). - Operands: The program then asks for two numbers (
num1
andnum2
).
Switch-Case Statement
- Addition (
+
): Addsnum1
andnum2
. - Subtraction (
-
): Subtractsnum2
fromnum1
. - Multiplication (
*
): Multipliesnum1
andnum2
. - Division (
/
): Dividesnum1
bynum2
. Ifnum2
is zero, the program outputs an error message for division by zero. - Default Case: If the user enters an invalid operator, the program outputs an error message.
Main Function
- The
main
function handles user inputs, calls the appropriate logic based on the operator, and displays the result.
Output Example
Example Output 1:
Enter an operator (+, -, *, /): + Enter two operands: 10 5 10.00 + 5.00 = 15.00
Example Output 2:
Enter an operator (+, -, *, /): / Enter two operands: 10 0 Error! Division by zero is not allowed.
Example Output 3:
Enter an operator (+, -, *, /): * Enter two operands: 4.5 3.2 4.50 * 3.20 = 14.40
Conclusion
This C program demonstrates how to implement a basic calculator that can perform addition, subtraction, multiplication, and division. It provides a user-friendly interface that handles different arithmetic operations and checks for errors, such as division by zero. The program is a simple yet practical example of using conditional statements and basic input/output in C programming.