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

Popular posts from this blog

The U.S. Census Bureau projects population based on the following assumptions: 1.One birth every 7 seconds 2.One death every 13 seconds 3.One new immigrant every 45 seconds Write a program that displays the population for each of the next five years. Assume the current population is 312,032,486 and one year has 365 days.

Write a program that prompts the user to enter the min- utes (e.g., 1 billion), and displays the number of years and days for the minutes. For simplicity, assume a year has 365 days

Write a program that reads the subtotal and the gratuity rate, then computes the gratuity and total. For example, if the user enters 10 for subtotal and 15% for gratuity rate, the program displays $1.5 as gratuity and $11.5 as total.