Kodewick
Start learning →
Basic←→0 / 70

Bags of flour

#048BasicFunctionsConditions4 min

A loaf needs 400 g of flour, and flour_for(loaves) already works out the grams. Write bags_needed(loaves): flour comes in 1000 g bags, so how many bags to buy (a part bag still means a whole bag)? You only call flour_for to get the grams; the rest is written.

Examples
bags_needed(5)→2
bags_needed(1)→1
The functionint bags_needed(int loaves)
main.c
int flour_for(int loaves) {
    return loaves * 400;
}

int bags_needed(int loaves) {
    int grams = 0;
    // your code here: call flour_for and put the grams in grams
    int bags = grams / 1000;
    if (grams % 1000 != 0) {
        bags++;
    }
    return bags;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.