Programming in C

(Barry) #1
Working with Arithmetic Expressions 31

int a = 100;
int b = 2;
int c = 25;
int d = 4;
int result;

result = a - b; // subtraction
printf ("a - b = %i\n", result);

result = b * c; // multiplication
printf ("b * c = %i\n", result);

result = a / c; // division
printf ("a / c = %i\n", result);

result = a + b * c; // precedence
printf ("a + b * c = %i\n", result);

printf ("a * b + c * d = %i\n", a * b + c * d);

return 0;
}


Program 4.2 Output


a - b = 98
b c = 50
a / c = 4
a + b
c = 150
a b + c d = 300


After declaring the integer variables a,b,c,d,and result, the program assigns the result
of subtracting bfrom ato resultand then displays its value with an appropriate printf
call.
The next statement


result = b * c;


has the effect of multiplying the value of bby the value of cand storing the product in
result.The result of the multiplication is then displayed using a printfcall that should
be familiar to you by now.
The next program statement introduces the division operator—the slash.The result of
4, as obtained by dividing 100 by 25, is displayed by the printfstatement immediately
following the division of aby c.


Program 4.2 Continued

Free download pdf