with rand function simple subtraction quiz generates
generating random number quiz
#include<iostream>
#include<ctime> // for time function
#include<cstdlib> // for rand and srand function
using namespace std;
int main()
{
// 1. generate two random single-digit integers
srand(time(0));
int number1 = rand() % 10;
int number2 = rand() % 10;
// 2. if number1<number 2 ,sawp number
if (number1 < number2)
{
int temp = number1;
number1 = number2;
number2 = temp;
}
//3. Prompt the student to "what is n1-n2?"
cout << " what is " << number1 << "-" << number2 <<"? ";
int answer;
cin >> answer;
//4. Grade the answer and display the result
if(number1 - number2 == answer)
cout << "you are correct!";
else
cout << " your answer is wrong. " << number1 << " - " << number2
<<" should be " << (number1-number2) << endl;
return 0;
}
output:-
what is 7-4? 3
you are correct!
what is 8-2? 3
your answer is wrong. 8 - 2 should be 6
Comments
Post a Comment
Any doubt about programming or questions