Answer to Question #261290 in C++ for meera

Question #261290

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.


1
Expert's answer
2021-11-06T03:15:59-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS