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.

Recommended Answers

All 3 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.