Division with random numbers

ok, so i made some adjustments and it works but now im trying to make it more shorter if possible? Here is my code...

case 4: while(true)
{
x=rand()%100;
y=rand()%99 + 1;
righttotal = x/y;
if ((x%y==0) && (y<x))
break;
}
while(x%y==0)
{
cout<<" "<<x<<endl;
cout<<"% "<<y<<endl;
cout<<"______"<<endl;
cout<<"= ";
cin>>thinktotal;
cout<<endl;
break;
}
while(thinktotal!=righttotal)
{
cout<<"Sorry try again"<<endl;
cin>>thinktotal;
}
if(thinktotal=righttotal)
cout<<"Thats Right!"<<endl;
break;
Last edited on
I would replace these lines:
x=rand()%100/2;
y=rand()%50/2;
righttotal=(x/y);

With:

1
2
3
4
5
6
7
8
while (true)
{
    x=rand()%100; // Random number between 0 and 99
    y=rand()%99 + 1; // Random number between 1 and 99 (not 0).
    righttotal = x/y;
    if ((x%y==0) && (y<x))  //Unless we have a quotient without a remainder and y < x, do it again.
        break;
}
Last edited on
The problem you highlighted:
while(righttotal!=righttotal%0)

(Number % 0) is rather strange. Essentially you are asking if a number is equal to the remainder of the same number once it has been divided by 0. You should also consider that you performed integer division, meaning that if you divide two integers in c++ (or c), the result will also be an integer (no remainder) so this really doesn't make sense.

I think you are looking for x%y==0 which means that x/y has no remainder.
Instead of generating random numbers for the division, generate the factors. For example (extra variables for clarity)

1
2
3
4
5
6
7
8
a = 1+rand()%15;
b = 1+rand()%15;

x = a*b;
y = b;
righttotal=x/y; //This wil be a, exactly, by definition.
...
...
Thanks man, thats what i meant but didnt know the coding for it.
Topic archived. No new replies allowed.