In this tutorial you will learn about the C program to perform addition, subtraction, multiplication and division and its application with practical example.
In this tutorial, we will learn to create a program to perform addition, subtraction, multiplication and division of two numbers using C programming language.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- C Data Types
- C Variables
- C Input Output
- C Operators
- C Typecasting
Program to perform basic arithmetic operations
In this program we will accept two operands from the user. Then, we will perform the basic arithmetic operations(addition, subtraction, multiplication and division) on the two operands. Later in the program we will display the result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> int main() { int num1, num2, add, subtract, multiply; float divide; printf("Enter the value of num1:"); scanf("%d",&num1); printf("Enter the value of num2:"); scanf("%d",&num2); add = num1 + num2; subtract = num1 - num2; multiply = num1 * num2; divide = num1 / (float)num2; //typecasting printf("Sum = %d\n",add); printf("Difference = %d\n",subtract); printf("Multiplication = %d\n",multiply); printf("Division = %.2f\n",divide); return 0; } |
Output:-
In the above program, we have first declared and initialized a set variables required in the program.
- num1 = it holds the first number value
- num2 = it holds the second number value
- add = it holds the sum of two numbers
- subtract = it holds subtraction result of two numbers
- multiply = it holds the multiplication result of two numbers
- divide = it holds the division result of two numbers
In the next statement user will be prompted to enter the two number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Then, we will perform the basic arithmetic operations(addition, subtraction, multiplication and division) on the two operands and assign the result to respective variable add, subtract, multiply and divide. Finally, we will be displaying the result using printf statement.