45.3k views
0 votes
Write a program that outputs a subtraction practice problem for a student, outputting the larger random number

first and the smaller random number second.

this is in C++ and i cant figure it out

User Csells
by
4.6k points

1 Answer

4 votes

Answer:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main(void)

{

srand(time(NULL));

int num1, num2;

int val;

while (true)

{

num1 = rand() % 100 + 1; //Random number between 1-100

num2 = rand() % 100 + 1; //Random number between 1-100

if (num1 >= num2)

{

printf("%d - %d = ", num1, num2);

scanf("%d",&val);

if (val == num1-num2)

{

printf("Correct!\\");

}

else

{

printf("Incorrect!\\");

}

}

else

{

printf("%d - %d = ", num2, num1);

scanf("%d",&val);

if (val == num2-num1)

{

printf("Correct!\\");

}

else

{

printf("Incorrect!\\");

}

}

}

}

Step-by-step explanation:

First, we create two random numbers and save the values. Then we check to see which value is larger. Based on that, we change how we display the format to the user. Then we check to see if the user input number is equivalent to the subtraction problem. If it is, we display Correct! Else, we display Incorrect!

Cheers.

User IceWhisper
by
5.4k points