In: Computer Science
USE C language :- The use of computers in education is referred
to as computer-assisted instruction (CAI). Write a program that
will help an elementary school student learn multiplication. Use
the rand function to produce two positive one-digit integers. The
program should then prompt the user with a question, such as
How much is 6 times 7?
The student then inputs the answer. Next, the program checks the student’s answer.
A separate function should be used to generate each new question. This function should be called once when the application begins execution and each time the user answers the question correctly.
Before ending the program, the program should display how many questions were right and how many questions were answered wrong at the first time it was answered.
Code:
#include <stdio.h>
#include <stdlib.h>
int question() {
int num1, num2, ans;
int c = 0;
num1 = rand() % 10;
num2 = rand() % 10;
printf("What is %d times %d ?\n ", num1, num2);;
scanf("%d", & ans);
while (ans != (num1 * num2)) {
c++;
printf("No. Please try again.\n");
printf("What is %d times %d ?\n", num1, num2);
scanf("%d", & ans);
}
printf("Very good!\n");
return c;
}
int main() {
int n;
int attempts = 0, corrects = 0;
int ch = 1;
while (ch == 1) {
attempts++;
n = question();
if (n == 0)
corrects++;
printf("Do you wish to continue(1 = y/ 0 = n)? ");
scanf("%d", &ch);
}
printf("\n\n\nNumber of questions answered correct on first try = %d", corrects);
printf("\nNumber of questions wrong correct on first try = %d", attempts - corrects);
return 0;
}
Output: