📅  最后修改于: 2023-12-03 14:59:37.716000             🧑  作者: Mango
Arithmetic operators are used to perform basic arithmetic operations in C programming. These operators include addition, subtraction, multiplication, division, and modulus. In this article, we will explore these arithmetic operators and their usage in C programming.
The addition operator is the simplest arithmetic operator, which is used to add two numbers. The syntax of the addition operator is as follows:
result = num1 + num2;
Here, num1
and num2
are the numbers that we want to add, and result
is the variable that stores the sum of num1
and num2
.
The subtraction operator is used to find the difference between two numbers. The syntax of the subtraction operator is as follows:
result = num1 - num2;
Here, num1
and num2
are the numbers that we want to subtract, and result
is the variable that stores the difference of num1
and num2
.
The multiplication operator is used to multiply two numbers. The syntax of the multiplication operator is as follows:
result = num1 * num2;
Here, num1
and num2
are the numbers that we want to multiply, and result
is the variable that stores the product of num1
and num2
.
The division operator is used to divide two numbers. The syntax of the division operator is as follows:
result = num1 / num2;
Here, num1
and num2
are the numbers that we want to divide, and result
is the variable that stores the quotient of num1
and num2
.
The modulus operator is used to find the remainder of division between two numbers. The syntax of the modulus operator is as follows:
result = num1 % num2;
Here, num1
and num2
are the numbers that we want to find the remainder of division between, and result
is the variable that stores the remainder of num1
divided by num2
.
Here is an example program that uses all these arithmetic operators:
#include <stdio.h>
int main() {
int num1, num2, result;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
result = num1 + num2;
printf("Sum: %d\n", result);
result = num1 - num2;
printf("Difference: %d\n", result);
result = num1 * num2;
printf("Product: %d\n", result);
result = num1 / num2;
printf("Quotient: %d\n", result);
result = num1 % num2;
printf("Remainder: %d\n", result);
return 0;
}
In this program, we take two input numbers from the user, and then we perform all the arithmetic operations using these input numbers. The results are then displayed on the screen.