cplusplus.com
C++ : Forum : General C++ Programming : devision
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs


questionsolved devision

mar11 (41)


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
bluezor (298)
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)
mar11 (41)
@bluezor: thanks a lot
firedraco (4744)
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.