-
Help with Modulus
Hi All,
Can one of you guys give me some advice on the following,
I have 2 float numbers and i want to change them into ints then
find the remander via modulus, this is what i have :-
Code:
#include <iostream.h>
#include <stdlib.h>
void main()
{
float float_num1;
float float_num2;
int int_num3;
int int_num4;
cout << "1st number:";
cin >> float_num1;
cout << "2nd number:";
cin >> float_num2;
int_num3 = (int)float_num1;
int_num4 = (int)float_num2;
cout << int_num3 % int_num4;
}
If i enter 98.33 for float1 & 15.22 for float2
The remainder shuld be 5 (if my maths is right)
But i get the result 8 in my program?
Can anyone see a problem?
Cheers
Boontune
-
>>Can anyone see a problem?
Your math is wrong. :-) 98 % 15 = 8, try it with a calculator if you want.
-
98%15 is indeed 8.
15*6 = 90
98-90 = 8
That said, your program still has a few flaws. main is not returning an int, like it should. You are not using stdlib.h yet it is still included, and you are using the depracated header includes (standard headers do not end in .h). Here is a more cleanly written version.
Code:
#include <iostream>
using namespace std;
int main()
{
float float_num1;
float float_num2;
int int_num3;
int int_num4;
cout << "1st number:";
cin >> float_num1;
cout << "2nd number:";
cin >> float_num2;
int_num3 = (int)float_num1;
int_num4 = (int)float_num2;
cout << int_num3 % int_num4 << endl;
return 0;
}
-
Wow, two of you posted while I was rewriting the program. AND Salem's post had the same exact (to the WORD, minus spacing and a period) sentence as mine (98 % 15 is indeed 8).
Pretty weird.
-
I know its a mess, i only through it together just to quickly show you guys!!
Oh crap, it is right!! hahaha
Sorry for wasting your time boys!
cya soon
Boontune