Use rand () to generate numbers in a division table

This is the result that I am trying to achieve:

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

I have already done addition, subtraction and multiplication, but I have encountered difficulties with division because I need the exact dividend and divider to divide in such a way that it would 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); } 
+7
c ++
source share
5 answers

You can simply select a random result and create a question.

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

then you ask how much is numerator / denominator

+3
source share

You can do

 float quo=(float)num/num2; printf("%f\n", quo); 

and print the exact result

If you want random numbers to be shared among you, you need something else.

also you do not initiate num until num/num2; and you do not check if num2 is null, so you can get a throw.

finaaly you can do something like

 num2=rand()%10; num=rand(); while((float)num/num2 != (float)(num/num2)) num=rand(); int quo=num/num2; 

if absolutely can only accept shared pairs

+2
source share

Just:

 num2=rand()%9+1; quo=rand()%10; num = quo * num2; printf("\n%d / %d = ",num,num2); 

In addition, you must move the srand() call before this loop. Otherwise, if someone answers the question too quickly, he will receive the same question again.

+1
source share

Testing with the module module:

 int test, result; test = 6%3; //test == 0 if(test == 0) result = 6/3; //test passes, assignment made test = 7%3; //test == 1 if(test == 0) result = 7/3; //test fails, assignment not made 

This guarantees a ratio leading to integer values.

Also , the random generator function can simplify the task:

 int randGenerator(int min, int max) { int random, trying; trying = 1; while(trying) { srand(clock()); random = (rand()/32767.0)*(max+1); (random >= min) ? (trying = 0) : (trying = 1); } return random; } 
0
source share

Answer:

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

clrscr (); srand ((unsigned) time (& t));

 for(x=0;x<5;x++) { num2=rand()%9; if(num2==1) { srand((unsigned) time(&t)); num=rand()%9; } else if(num2==2) { srand((unsigned) time(&t)); num=(rand()%9+1)*2; } else if(num2==3) { srand((unsigned) time(&t)); num=(rand ()%9+1)*3; } else if(num2==4) { srand((unsigned) time(&t)); num=(rand()%9+1)*4; } else if(num2==5) { srand((unsigned) time(&t)); num=(rand()%9+1)*5; } else if(num2==6) { srand((unsigned) time(&t)); num=(rand()%9+1)*6; } else if(num2==7) { srand((unsigned) time(&t)); num=(rand()%9+1)*7; } else if(num2==8) { srand((unsigned) time(&t)); num=(rand()%9+1)*8; } else if(num2==9) { srand((unsigned) time(&t)); num=(rand()%9+1)*9; } else if(num2==10) { srand((unsigned) time(&t)); num=(rand()%9+1)*10; } else { clrscr(); printf(""); } quo=num/num2; clrscr(); gotoxy(30,14); printf("\n%d / %d = ",num,num2); printf("\nAns: "); scanf("%d",&ans); 

}

0
source share

All Articles