devision



Hi all,

assuming this code:

1
2
3
4
5
6

int a;

a = 90 / 20;

cout << "a"<<a<<endl; // output is 4 


How can i declare the variable if i want :

1- to get 90 /20 = 4.5

2- to get 90/20 = 5

thanks for each advance
You can declare it as double a; for it to be able to take decimal points.
You can round up / round down a value using <cmath>'s two functions -
* ceil - http://www.cplusplus.com/reference/clibrary/cmath/ceil/ (rounding up)
* floor - http://www.cplusplus.com/reference/clibrary/cmath/floor/ (rounding down)
@bluezor: thanks a lot
Also, you need to change either 90 or 20 to a float/double (otherwise you will get 4 because of integer division).
Topic archived. No new replies allowed.