In this tutorial you will learn about the C program for Simple Calculator and its application with practical example.
C Program for Simple Calculator
In this tutorial, we will learn to create a C program. After that, will perform Simple Calculator in C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C Programming.
- Switch Case in C programming
Program for Simple Calculator:-
In today’s tutorial, we will create a program. That will do the Addition, Subtraction, Multiplication, and Division operations with the help of Arithmetic operators in C programming.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
1. Declaring the variables for the program. 2. Taking the input numbers from the user in number format. 3. Adding the numbers and storing it in a variable. 4. Subtracting the numbers and storing it in a variable. 5. Multiplying the numbers and storing it in a variable. 6. Dividing the numbers and storing it in a variable. 7. Printing the Outputs. 8. End program. |
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <stdio.h> int main() { //declaring the rwequired variable for the program char op; double first, second; //taking the input variable for the program printf("Enter an operator (+, -, *, /): "); scanf("%c", &op); printf("Enter two operands: "); scanf("%lf %lf", &first, &second); //Calculating the arithmetic operations using switch case switch (op) { case '+': printf("%.1lf + %.1lf = %.1lf", first, second, first + second); break; case '-': printf("%.1lf - %.1lf = %.1lf", first, second, first - second); break; case '*': printf("%.1lf * %.1lf = %.1lf", first, second, first * second); break; case '/': printf("%.1lf / %.1lf = %.1lf", first, second, first / second); break; // operator doesn't match any case constant default: printf("Error! operator is not correct"); } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- op = it will hold the input character value from the user.
- first = it will hold the input number value from the user.
- second = it will hold the input number value from the user.
Taking Input numbers from the user.
Performing simple calculations.
Printing the output.