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

Write a program that reads in the radius and length of a cylinder and computes the area and volume using the following formulas: area = radius * radius * π volume = area * length

Write a program that prompts the user to enter two points (x1, y1) and (x2, y2) and displays their distance between them.The formula for computing the distance is

Area and perimeter of an equilateral triangle c++ program