Site icon DataFlair

C++ Project – Number Guessing Game

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Game for Guess Random Number
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#define clrscr() system("cls")
using namespace std;
int main()
{
    clrscr();
    srand(time(NULL));
    int comp,user;
    int count=5;
    int i=0,choice;
do
{
    comp=rand()%100;
    while(count)
    {
        i++;
       cout<<"\n Enter a number for guess: You have "<< count <<" chance left";
        cin>>user;
     if(user==comp)
     {
         cout<<"\n ******Congrats You won the game in "<< i <<" Chances *****";
         break;
     }
     else
      if(user>comp)
      cout<<"\n  Your number is grater";
      else
      cout<<"\n  Your number is less" ;
      count--;
    }
    if(count==0)
    {
        cout<<"\n Oops you lose the game.. all chances finish !";
        cout<<"\n Computer genrated no is : "<<comp;
    }
 cout<<"\n You want to play more.!(For Yes Press 1 for No Press 0)";
 cin>>choice;
}while(choice!=0);   
cout<<"\n Thanks for playing Bye Bye !";
    return 0;
}

 

Exit mobile version