Write a C++ program that generates a random number from 0 to 10 and saves it in an integer Correct. The program prompts the user to guess the number between 0 and 10. If the user guesses the number that matches Correct, then the program outputs the message “you guessed the number correctly”. Otherwise, the program checks whether the guessed number is less than Correct and outputs the message” your number is lower than the number. Guess again”. If not, the program will output “your number is higher than the number. Guess again”. The program prompts the user to enter another number until the user enters the correct number.
#include <iostream>
#include<ctime> //For time()
#include<cstdlib> //For rand() and srand()
using namespace std;
int main() {
srand(time(NULL));
//generates a random number from 0 to 10 and saves it in an integer Correct.
int Correct=rand()%11;
int userNumber=-1;
//The program prompts the user to enter another number until the user enters the correct number.
while (userNumber!=Correct){
//The program prompts the user to guess the number between 0 and 10.
cout<<"Ente the number between 0 and 10: ";
cin>>userNumber;
//If the user guesses the number that matches Correct, then the program outputs the message "you guessed the number correctly".
//Otherwise, the program checks whether the guessed number is less than Correct and outputs the message
//"your number is lower than the number. Guess again".
if(userNumber<Correct){
cout<<"\nYou number is lower than the number. Guess again\n\n";
}
if(userNumber==Correct){
cout<<"\nYou guessed the number correctly\n\n";
}
//If not, the program will output "your number is higher than the number. Guess again".
if(userNumber>Correct){
cout<<"\nYou number is higher than the number. Guess again\n\n";
}
}
cin>>Correct;
return 0;
}
Comments
Leave a comment