ARITHMETIC OPERATORS
The symbols of the arithmetic operators are:-
Operation | Operator | Comment | Value of Sum before | Value of sum after |
Multiply | * | sum = sum * 2; | 4 | 8 |
Divide | / | sum = sum / 2; | 4 | 2 |
Addition | + | sum = sum + 2; | 4 | 6 |
Subtraction | - | sum = sum -2; | 4 | 2 |
Increment | ++ | ++sum; | 4 | 5 |
Decrement | -- | --sum; | 4 | 3 |
Modulus | % | sum = sum % 3; | 4 | 1 |
The following code fragment adds the variables loop and count together, leaving the result in the variable sum
sum = loop + count;
Note: If the modulus % sign is needed to be displayed as part of a text string, use two, ie %%
#include <stdio.h> main() { int sum = 50; float modulus; modulus = sum % 10; printf("The %% of %d by 10 is %f\n", sum, modulus); } Sample Program Output The % of 50 by 10 is 0.000000
CLASS EXERCISE C5
What does the following change do to the printed output of the
previous program?
printf("The %% of %d by 10 is %.2f\n", sum, modulus);