In: Computer Science
Write a program named problem.c to generate addition and subtraction math problems for a 4th grader.
1. Your program will first ask user how many problems they want to do.
2. Repeat the following until you give user the number of problems that the user asks for.
a. Display the question number
b. Generate a addition or subtraction problem using two random generated two digits (0-99).
- The odd number of problems (1,3,5,…) will be addition problems.
- The even number of problems (2,4,6,…) will be subtraction problems. For subtraction problem, swap two numbers if the first number is smaller than the second number.
c. Display the problem, then ask user to answer. If the user answers correctly the first time, the user gets 10 points for the problem. If the user does not answer correctly, you print the problem again, and ask the user to try again. If the user answers correctly second time, the user gets 5 points for this problem. You then display the total points that user gets.
4. Calculate and display the percentage. a. If the percentage is above or equal 93%, display “Excellent”. b. If the percentage is above or equal 87%, display “Very good”. c. Otherwise, print “Keep working hard.”
Output shown look like:
How many problems would you like to do today?4
Ok! You will be given 4 math problems.
Good luck with them! Press enter to start.
Question 1
92 + 11 = 103
Correct! 10 points. Total points: 10
Question 2
95 - 10 = 85
Correct! 10 points. Total points: 20
Question 3
31 + 56 = 87
Correct! 10 points. Total points: 30
Question 4
32 - 29 = 2
Wrong. Please try again.
32 - 29 = 3
Correct. 5 points. Total points: 35
You got 87.50%! Very Good!
Thank you for using my math problem generator!
#include<stdio.h>
#include<stdlib.h>
int main()
{
//variable declaration
int a,b,n,ans,point=0,count=0,i;
double percentage;
//ask for number of problems
printf("\n How many problems would you like to
do today?");
scanf("%d",&n);
//loop will continue from 1 to n
for(count=1;count<=n;count++)
{
a=rand()%100; //generate first
random number between 0 to 99 inclusive
b=rand()%100; //generate second
random number between 0 to 99 inclusive
if(count%2==0) //check for even
number
{ i=1; //set the counter for number
of attempts
printf("\n Question
%d",count); //display the question number
if(a<b) //swap if
a<b
{
a=a+b;
b=a-b;
a=a-b;
}
while(i<3) //loop
will continue 2 times at most
{
printf("\n %d - %d = ",a,b); //display the
question
fflush(stdin);
scanf("%d",&ans);//read answer
if(ans==(a-b) && i==1) //check the validity of answer in
first attempt
{
point = point+10; //update
the point
printf("\n Correct! 10
points, Total points %d",point);
break;//terminate loop
}
else
if(ans==(a-b) &&
i==2)//check the validity of answer in second attempt
{
point = point+5;//update the
point
printf("\n Correct! 5 points,
Total points %d",point);
break;//terminate loop
}
else //display message for
wrong answer
printf("\n Wrong. Please Try
again");
i++; //increment the counter
for while loop
}
}
else
{
i=1;
printf("\n Question
%d",count);
while(i<3)//loop
will continue 2 times at most
{
printf("\n %d + %d = ",a,b);//display the
question
fflush(stdin);
scanf("%d",&ans);//read answer
if(ans==(a+b) && i==1) //check the validity of answer in
first attempt
{
point = point+10;//update the
point
printf("\n Correct! 10
points, Total points %d",point);
break;//terminate loop
}
else
if(ans==(a+b) &&
i==2)//check the validity of answer in second attempt
{
point = point+5;//update the
point
printf("\n Correct! 5 points,
Total points %d",point);
break;//terminate loop
}
else//display message for
wrong answer
printf("\n Wrong. Please Try
again");
i++;//increment the counter
for while loop
}
}
}
//display the final point
printf("\n Total Points : %d",point);
//computing the percentage
percentage = (double)(point)/(n*10);
percentage = percentage * 100;
//block for percentage above or equal to 93
if(percentage>=93)
printf("\n You got %5.2lf!
Excellent!",percentage);
else
if(percentage>=87) //block for percentage above or
equal to 87
printf("\n You got %5.2lf! Very
Good!",percentage);
else
printf("\n You got %5.2lf! Keep working very
hard",percentage);
//display the thank you message
printf("\n Thank you for using my math problem
generator!");
}
OUTPUT