Contact Learn C
Copy Program Share Program

Program 88:Number Guessing Game

Program 88:
#include<stdio.h>
main()
{
 int i,num=51,flag=1,guess,count=0;
 printf("Guess the number\n");
 scanf("%d",&guess);
do
 {
 
  if(num==guess)
  {
   flag=0;
  }
  else if(guess<num)
  {
   flag=1;
   printf("Your guess is lower than the number\n");
   count++;
  }
  else
  {
    flag=1;
   printf("Your guess is greater than the number\n");
   count++;
  }
  
  if(flag==1)
  {
    printf("Sorry! Please guess it again\n");
   scanf("%d",&guess);
  }
 } while(flag);
 
 printf("Congrats! You guessed the number %d\n",num);
 printf("Number of trails: %d\n",count);
 
 
}

Explanation:

  1. The program starts with initializing
    • num → To store number that user has to guess
    • i Temporary variable
    • flag→ To store  1 if user didnot guess correctly and 0 if user guessed it correctly
    • count→ To store number of attempts/trails made by user before guessing the number correctly
  2. printf("Guess the number\n");
     scanf("%d",&guess);
    Taking guess number from user.
  3. do
    { 
    
    } while(flag);
    
    Used do While because the loop should execute atleast once and has to be executed until user guess it correctly.
  4. Main Logic goes here:
     
    if(num==guess) {
       flag=0;
      }
      else if(guess<num)
      {
       flag=1;
       printf("Your guess is lower than the number\n");
       count++;
      }
      else
      {
        flag=1;
       printf("Your guess is greater than the number\n");
       count++;
      }
      
      if(flag==1)
      {
        printf("Sorry! Please guess it again\n");
       scanf("%d",&guess);
      }
  5. As num=51. Let for the first guess given by user be guess=5.
    • Iteration 1:
      • if(num==guess) {
           flag=0;
          }
        Since 51 is not equal to 5 this if part won't execute
      • 2nd if part guess<num(5<51) is true so flag=1 and prints that user guessed lower number and count will be incremented by 1.So now count=1 indicating 1 wrong attempt.
      • Now flag=1. So
         
        if(flag==1) {
            printf("Sorry! Please guess it again\n");
           scanf("%d",&guess);
          }
        this if part at the end of loop will be executed and takes guess number again from user
      • Now since flag=1 while loop continues to next iteration
      • Now lets take guess=78
    • Iteration 2
      • if(num==guess) {
           flag=0;
          }
        Since 51 is not equal to 78 this if part won't execute
      • 2nd if part guess<num(78<51) is false  this if part  also won't execute
      • remaining this is else part
          
        else{
            flag=1;
           printf("Your guess is greater than the number\n");
           count++;
          }
        So again flag=1 and prints guess number is greater.Now count will be incremented by 1.So now count=2. So now user attempted 2 times and guessed wrong one
      • Now flag=1. So
         
        if(flag==1) {
            printf("Sorry! Please guess it again\n");
           scanf("%d",&guess);
          }
        this if part at the end of loop will be executed and takes guess number again from user
      • Now since flag=1 while loop continues to next iteration
    • Iteration 3: Now lets take guess=51
      • if(num==guess) {
           flag=0;
          }
        Since 51 is equal to 51 .flag will become zero.flag=0
      • Reamining if parts will be skipped and while loop will terminate.
  6. printf("Congrats! You guessed the number %d\n",num);
     printf("Number of trails: %d\n",count);
    Finally it displays Success message to user and prints number of times user attempted to guess the number correctly and in this case it is 2.


 Output:

Number Guessing Game









Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide