Hey guys, i been looking through at questions regarding roman numerals and i have a question. i ran some of the coding which i found and i dont quite understand this part..

int number = 3000;
int number2 , i ;
      number2 = number / 1000;
for(i = 1; i <= number2; i++)
cout << 'M';

When i run this, the expected result will be MMM. but when i changed , int number = 3001 , i still get back the result of MMM. Why is this? I would have expected the program not to give any solution at all?

Am i right to say that in c++ , when you use division ( /) , it will keep dividing till the point it is unable to divide any smaller?

Hope you guys can help explain this to me.

8 Years
Discussion Span
Last Post by metdos

In most languages integer division returns the floor or rounded down integer so 3001/1000 = 3.xxx which as a rounded down integer is just 3.


This number2 = number / 1000; is rounded to an integer.
If you want to have a floating point result use float number2 = number / 1000;


Actually you need to

float number2 = (float)number / 1000;

for real answer, because, 3001/1000 will give you 3, and it will cast it to float as 3.0

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.