Answer in brief
Arithmetic operators perform addition, subtraction, multiplication, division and remainder calculations.
Operators and examples
- Addition (+): 7 + 2 gives 9.
- Subtraction (-): 7 - 2 gives 5.
- Multiplication (*): 7 * 2 gives 14.
- Division (/): 7 / 2 gives 3 when both operands are integers. 7.0 / 2 gives 3.5.
- Remainder (%): 7 % 2 gives 1, the remainder after integer division. Both operands must have integer type.
Integer and floating-point division
Integer division discards the fractional part towards zero. Thus -7 / 2 gives -3. If a floating-point operand is present, floating-point division is used after the required conversions. To calculate a fractional average from integers, write (double)total / count with a non-zero count.
Example
For a = 17 and b = 5, a / b is 3 and a % b is 2. The relationship is 17 = 3 * 5 + 2. This explains the difference between quotient and remainder.
Important points
Never divide by zero or use zero as the divisor of %. Keep results within the allowed type range: signed integer overflow has no defined result in C. Integer division must also have a representable result. Floating-point calculations may contain small rounding errors.
C program and output
/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int main(void)
{
int a = 7, b = 2;
printf("sum = %d\n", a + b);
printf("difference = %d\n", a - b);
printf("product = %d\n", a * b);
printf("integer division = %d\n", a / b);
printf("remainder = %d\n", a % b);
printf("real division = %.1f\n", (double)a / b);
return 0;
}
Sample input and output
This example uses values already written in the program.
Output
sum = 9
difference = 5
product = 14
integer division = 3
remainder = 1
real division = 3.5
Key points
- 7 / 2 and 7.0 / 2 have different results.
- % is an integer operator.