Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use rand() to generate numbers in division table

Tags:

c++

This is the output I'm trying to achieve:

(randomized var1)/(randomized var2)=
Ans:(var ans)

I've already done addition, subtraction and multiplication but I encountered difficulty with division because I need the exact dividend and divisor to divide so that it would not be difficult to answer.

Example:

40/5=
Ans: 8

not this one:

7/5=
ans: float value

Here is my code:

int x,num,num2,ans,quo,score=0;
time_t t;

clrscr();

for(x=0;x<5;x++)
{
    srand((unsigned) time(&t));
    num2=rand()%10;
    quo=num/num2;

    if(num2==1)
    {
        num=rand()%9;
    }
    else if(num2==2)
    {
        num=2 + (2 * rand ()) %18; //this should return a value divisible by 2(ranges 0-18)
    }
    else if(num2==3)
    {
        num=rand()% //this should return a value divisible by 3 only (ranges 0-27)
    }
    else if(num2==4)
    {
        num=rand()% //this should return a value divisible by 4 only (ranges 0-36)
    }
    else if(num2==5)
    {
        num=rand()% //this should return a value divisible by 5 only (ranges 0-45)
    }
    else if(num2==6)
    {
        num=rand()% //this should return a value divisible by 6 only (ranges 0-54)
    }
    else if(num2==7)
    {
        num=rand()% //this should return a value divisible by 7 only (ranges 0-63)
    }
    else if(num2==8)
    {
        num=rand()% //this should return a value divisible by 8 only (ranges 0-72)
    }
    else if(num2==9)
    {
        num=rand()% //this should return a value divisible by 9 only (ranges 0-81)
    }
    else if(num2==10)
    {
        num=rand()% //this should return a value divisible by 10 only (ranges 0-90)
    }
    else
    {
    }

    gotoxy(30,14);
    printf("\n%d / %d = ",num,num2);
    printf("\nAns: ");
    scanf("%d",&ans);
} 
like image 270
Hasmine Avatar asked Oct 04 '22 04:10

Hasmine


1 Answers

You can just pick a random result and create the question

denominator = 14 (randomly chosen)
result = 21 (randomly chosen)

numerator = denominator * result

you then ask how much is numerator / denominator

like image 181
6502 Avatar answered Oct 11 '22 03:10

6502