Skip to content
This repository was archived by the owner on Dec 1, 2022. It is now read-only.

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

README.md

Module 1

Back to Class 2 Prep

Exercise: How many oranges?

In a file called howmanyoranges.c write a program that asks the user how many orange slices they want to eat, then prints out how many oranges the user should buy so they have enough slices. Assume there are 8 slices in each orange!

A run of your program should look like this:

$ ./howmanyoranges
How many orange slices do you want to eat? 15
You should buy 2.0 oranges!
$ ./howmanyoranges
How many orange slices do you want to eat? 17
You should buy 3.0 oranges!

How can we do this? We need to buy enough oranges so that there are at least as many slices available as the user wants to eat this week. We can do this by dividing the number of slices they want by the number of slices in each orange (8). For example, 17 / 8.0 yields 2.215 oranges. But because we don't live in a world where one can buy fractional amounts of things (except donuts of course), we need to round up to the nearest next integer.

To do this, we'll need to use the ceil function found here, in the math.h library.

float x = ceil(8.2);
// the value of x is now 9.0

Finally, don't forget to #include the <math.h> library at the top of your file!