Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Programming Question: Design a C program (Please not C++) to teach multiplicat

ID: 3798328 • Letter: C

Question

C Programming Question:

Design a C program (Please not C++) to teach multiplication tables. Use the function rand( ) to generate two integer numbers between 1 to 9. Your program will first ask a question, such as: “6 × 7 = ?” The student should then type the answer on the same line. Then your program checks the student’s answer.

If it is correct, print “Very good!” and then ask another multiplication question. If the answer is wrong, print “No. Please try again.” and then let the student try the same question again repeatedly. The loop will end if the student enters “-1” as the answer.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int a,b,c;
srand (time(NULL));
while(1){
a = rand() % 10 + 1;
b = rand() % 10 + 1;
while(1){
printf("%d X %d = ", a,b);
scanf("%d", &c);
if(c==-1){
exit(0);
}
if( c == a *b){
printf("Very good! ");
break;
}
else {
printf("No. Please try again. ");
}
}

}
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                                             

6 X 6 = 34                                                                                                                                                                                                                                                               

No. Please try again.                                                                                                                                                                                                                                                    

6 X 6 = 36                                                                                                                                                                                                                                                               

Very good!                                                                                                                                                                                                                                                               

1 X 5 = 5                                                                                                                                                                                                                                                                

Very good!                                                                                                                                                                                                                                                               

7 X 3 = 22                                                                                                                                                                                                                                                               

No. Please try again.                                                                                                                                                                                                                                                    

7 X 3 = 21                                                                                                                                                                                                                                                               

Very good!                                                                                                                                                                                                                                                               

9 X 7 = -1