Number Guessing Game in C#

This is a C# Program to perform a number guessing game.

Problem Description

This C# Program Performs a Number Guessing Game.

Problem Solution

Here a Simple number guessing game is created.

Program/Source Code

Here is source code of the C# Program to Perform a Number Guessing Game. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Perform a Number Guessing Game
 */
using System;
using System.Collections.Generic;
using System.Text;
class Program
{
     static void Main(string[] args)
      {
           while (true)
            {
                int randno = Newnum(1, 101);
                int count = 1;
                while (true)
                {
                    Console.Write("Enter a number between 1 and 100(0 to quit):");
                    int input = Convert.ToInt32(Console.ReadLine());
                    if (input == 0)
                        return;
                    else if (input < randno)
                    {
                        Console.WriteLine("Low, try again.");
                        ++count;
                        continue;
                    }
                    else if (input > randno)
                    {
                        Console.WriteLine("High, try again.");
                        ++count;
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("You guessed it! The number was {0}!", 
                                           randno);
                        Console.WriteLine("It took you {0} {1}.\n", count, 
                                           count == 1 ? "try" : "tries");
                        break;
                    }
                }
            }
 
        }
        static int Newnum(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }
    }
}
Program Explanation

In this C# program, we are reading a number between 1 and 100, using ‘input’ variable. Nested if else condition statement is used to check the value of ‘input’ variable is equal to 0, if the condition is true then execute if condition statement.

advertisement
advertisement

Otherwise, if the condition is false then execute the elseif statement. Check the condition that the value of ‘input’ variable is greater than the value of ‘randno’ variable. If the condition is true then execute the statement and print the statement as the value is low try again. Decrement the value of ‘count’ variable.

Otherwise, if the condition is false then execute another elseif statement. Check the value of ‘input’ variable is greater than the value of ‘randno’ variable. If the condition is true then execute the elseif statement.

Print the statement as the value is high try again and decrement the value of ‘count’ variable. Otherwise if the condition is false then execute the else statement and print the guessed number.

Runtime Test Cases
 
Enter a number between 1 and 100(0 to quit) : 56
Low,try again.
Enter a number between 1 and 100(0 to quit): 67
high,try again.
Enter a number between 1 and 100(0 to quit): 59
You guessed it! The number was 59
It took you 2 tries!!!

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.