In this video tutorial we shall perform Addition, Subtraction, Multiplication and Division of numbers based on user input.
Related Read:
Basic Arithmetic Operations In C
Addition of 2 Numbers: C
Subtraction of 2 Numbers: C
Multiplication of 2 Numbers: C
Division of 2 Numbers: C
else if statement in C
Simple Calculator Program In C
[youtube https://www.youtube.com/watch?v=QO6pCvM3xSQ]
Working of Calculator Program
We display list of operation one can perform. That is,
1. Addition
2. Subtraction
3. Multiplication
4. Division
We ask the user to input his / her choice for arithmetic operation. If the user selects 1, then we ask the user to enter 2 integer numbers to perform addition operation. Once the user enters 2 integer numbers we add and display the result on the screen.
If the user enters wrong choice, we ask the user to enter proper choice.
Student Grade Calculation using else-if clause
#include < stdio.h > int main() { int a, b, choice; printf("Enter your choice\n"); printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n\n"); scanf("%d", &choice); if(choice > 4) { printf("You entered wrong choice\n"); } else { printf("Enter 2 integer numbers\n"); scanf("%d %d", &a, &b); } if(choice == 1) { printf("Addition of %d and %d is %d\n", a, b, (a+b)); } else if(choice == 2) { printf("Subtraction of %d and %d is %d\n", a, b, (a-b)); } else if(choice == 3) { printf("Multiplication of %d and %d is %d\n", a, b, (a*b)); } else if(choice == 4) { if(b != 0) printf("Division of %d and %d is %d\n", a, b, (a/b)); else printf("Number can not be divided by 0\n"); } else { printf("Please enter any of these no 1, 2, 3, 4\n"); } return 0; }
Output 1:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
1
Enter 2 integer numbers
3
2
Addition of 3 and 2 is 5
Output 2:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
2
Enter 2 integer numbers
8
3
Subtraction of 8 and 3 is 5
Output 3:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
3
Enter 2 integer numbers
5
5
Multiplication of 5 and 5 is 25
Output 4:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
4
Enter 2 integer numbers
10
2
Division of 10 and 2 is 5
Output 5:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
4
Enter 2 integer numbers
0
12
Division of 0 and 12 is 0
Output 6:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
4
Enter 2 integer numbers
10
0
Number can not be divided by 0
Output 7:
Enter your choice
1. Addition
2. Subtraction
3. Multiplication
4. Division
50
You entered wrong choice
Please enter any of these no 1, 2, 3, 4
Note: In division section(that is, choice 4) you can see nesting of if else statement. You can know more about nested if-else statements in this program: Nested if else Statement In C
Note: We can write the same program within while loop and ask the user if he wants to continue working inside calculator application. This avoids the need to exit and re-run the program. We’ll publish the code and video tutorial for the same in coming days. Stay subscribed to our YouTube Channel and blog.
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert