C arithmetic operators
Created By: chatGPT
In C programming, arithmetic operators are used to perform basic mathematical operations. These include addition, subtraction, multiplication, division, and the modulus operation. Here’s a brief overview of each operator:
Addition (+): This operator adds two operands together.
int sum = a + b;
Subtraction (-): This operator subtracts the second operand from the first.
int difference = a - b;
Multiplication (*): This operator multiplies two operands.
int product = a * b;
Division (/): This operator divides the first operand by the second. It’s important to note that if both operands are integers, the result will also be an integer (the fractional part is discarded).
int quotient = a / b;
Modulus (%): This operator returns the remainder of the division of two integers.
int remainder = a % b;
Here is an example that demonstrates all of these operators in a simple program:
In the above program, you can see how each arithmetic operator is utilized to compute values based on the variables #include <stdio.h>
int main() {
int a = 10;
int b = 3;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
printf("Sum: %d\n", sum);
printf("Difference: %d\n", difference);
printf("Product: %d\n", product);
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);
return 0;
}
a
and b
. When executed, this program will output the results for all these operations, making it clear how arithmetic works in C.