Create a C application that plays guess the number as follow
Create a C# application that plays “guess the number” as follows. Your application chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The application displays the prompt:
Guess a number between 1 and 1000.
The player inputs a first guess. If the players guess is incorrect, your application should display:
Too high. Try again.
or
Too low. Try again.
to help the player “zero in” on the correct answer. The application should prompt the user for the next guess. When the user enters the correct answer, display:
Congratulations. You guessed the number!
Solution
Code:
using System.IO;
using System;
class Program
{
static void Main()
{
do
{
Console.WriteLine(\"Guess a number between 1 to 1000\");
string str = Console.ReadLine();
double userval = double.Parse(str);
Random random = new Random();
double minimum =1;
double maximum =1000;
double sysval = random.NextDouble() * (maximum - minimum) + minimum;
if(userval>sysval)
Console.WriteLine(\"Too high! try again\");
else if(userval<sysval)
Console.WriteLine(\"Too high! try again\");
else
{
Console.WriteLine(\"Congratulations. You guessed the number!\");
break;
}
}while(true);
}
}
