Solution for Exercise 11-22

ex1122

#include <stdio.h>

int main()
{
    int result;

    result=20-5*2+42/6;
    printf("20-5*2+42/6=%d\n",result);
    return(0);
}

Output

20-5*2+42/6=17

Notes

* Sure, you could have just displayed the result, but why not add the equation?

* Both 5*2 and 42/6 are calculated first. The result is 20-10+7, which is read left-to-right by both humans and computers for a result of 17.

* Yes, normally you need a float variable because division is involved. If you declared the variable as a float - great! If you used the %.0f placeholder to output the value - better! But I wrote the equation so that the division operation comes out evenly - even if you work the problem from left-to-right.