Friday, October 29, 2021

Programs to Guess a number

Programs to Guess a number

Programs to Guess a number Which is generated Randomly by Computer

Questions
Create a program in any third generation language which asks the user to guess a number between 1 and 10, compare it to a random number which was generated by the computer. The program should provide three chances to the user to input. If the user enter the number correctly the program should end and print the message "Congratulations you won the game...." If he fails to guess the number then program should end and print the message "You lost the game.."

Python, Pascal, C# and Java Program are given below as the solutions for the above problem:

 
Python Program 

from random import randint
randnum=randint(1,10)
count=0
found=False
while ((found!=True) and (count <3)):
    guessnum=int(input("Enter a number between 1 and 10..."))
    if (guessnum==randnum):
        found=True
    count=count+1

if (found==True):
    print("Congratulations ...You won the game!")
else:
    print("You lost the game!")


Sample Output if we guess all three chances uncorrecly 

Enter a number between 1 and 10...2
Enter a number between 1 and 10...1
Enter a number between 1 and 10...7
You lose the game!


Sample Output if we guess second time correctly 

Enter a number between 1 and 10...8
Enter a number between 1 and 10...3
Congratulations ...You won the game!

 
Pascal Program 

program Project1;

var
  randnum, count, guessnum : byte;
  found:boolean;
begin
  randomize; {to initialize the random number generator}
  randnum:= random(10);
  count:=0;
  found:=false;
  while ((found <>True) and (count <3)) do
  begin
       write('Enter a number between 1 and 10...');
       readln(guessnum);
       if (guessnum=randnum) then
       begin
            found:=True;
       end;
       count:=count+1;
  end;
  if (found=True) then
    writeln('Congratulations ...You won the game!')
  else
    writeln('You lost the game!');

  readln;
end.

 
C# Program 

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, count, gnum;
            Random r = new Random();
            num = Convert.ToInt32(r.Next(1,10));
            Boolean found;
            found = false;
            count = 0;
            while ((found!= true) && (count <3))
            {
                Console.Write("Enter a Number between 1 and 10?");
                gnum=Convert.ToInt32(Console.ReadLine());
                if (gnum==num)
                {
                    found = true;
                
                }
                count = count + 1;
            }
            if (found==true)
            {
                Console.WriteLine("Congratulations ...You won the game!");
            }
            else
            {
                Console.WriteLine("You lost the game!");
            }
            Console.ReadLine();
        }
    }
}


 
Java Program

import java.lang.Math;
import java.util.*;
public class Myguess
{
	public static void main(String[] args)
	{
		int num, count, gnum;
		num=(int)(Math.random()*10);	     
		Scanner sc= new Scanner(System.in);            
		Boolean found;
		found = false;
		count = 0;
		while ((found!= true) && (count <3))
		{
			System.out.print("Enter a Number between 1 and 10?");
			gnum=sc.nextInt();;
			if (gnum==num)
			{
				found = true;  
			}
			count = count + 1;
		}
		if (found==true)
		{
			System.out.print("Congratulations ...You won the game!");
		}
		else
		{
			System.out.print("You lost the game!");
		}            
	}    
}


No comments:

Post a Comment