Percentage
Keep precision by ordering the operations correctly. An easy embedded C problem (Variables and operators), run and graded in the browser.
easy · Variables and operators · division · debugging
Return what percentage part is of whole, with the fraction dropped. whole is never 0.
The starter looks right and returns 0 almost every time. Fix it.
Starter code
int percent(int part, int whole) {
return part / whole * 100;
}Example tests
One quarter
printf("%d\n", percent(1, 4));Expected output:
25Three quarters
printf("%d\n", percent(3, 4));Expected output:
75Learn it first
- Variables and operators · Storing values, doing arithmetic, comparing, and the two surprises of integer maths.
More Variables and operators problems
- Average of three · Add three values and divide.
- Last digit · Use the remainder operator.
- Celsius to Fahrenheit · Fix a formula that divides too early.
- Time in seconds · Combine several values into one total.
- Even or odd · Produce a yes-or-no answer as 1 or 0.
- Operator precedence · Fix an expression that is evaluated in the wrong order.
- Does it fit in a byte? · Combine two comparisons with a logical operator.