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 a number in feet, converts it to meters, and displays the result. One foot is 0.305 meter.

Body Mass Index (BMI) is a measure of health based on height and weight. You can calculate your BMI by taking your weight in kilograms and dividing it by the square of your height in meters

Write a program that prompts the user to enter the coordinates of two points (x1, y1) and (x2, y2), and displays the slope of the line that connects the two points. The formula of the slope is (y2 - y1)/(x2 - x1).