C program for number Guessing Game.
Click to check video 👆👆👆👆👆👆
Output:
Guess the number
45
Your guess is lower than the number
Sorry! Please guess it again
56
Your guess is greater than the number
Sorry! Please guess it again
47
Your guess is lower than the number
Sorry! Please guess it again
54
Your guess is greater than the number
Sorry! Please guess it again
50
Your guess is lower than the number
Sorry! Please guess it again
52
Your guess is greater than the number
Sorry! Please guess it again
51
Congrats! You guessed the number 51
Number of trails: 6Explanation:
Click to check video 👆👆👆👆👆👆
#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); }
Guess the number
45
Your guess is lower than the number
Sorry! Please guess it again
56
Your guess is greater than the number
Sorry! Please guess it again
47
Your guess is lower than the number
Sorry! Please guess it again
54
Your guess is greater than the number
Sorry! Please guess it again
50
Your guess is lower than the number
Sorry! Please guess it again
52
Your guess is greater than the number
Sorry! Please guess it again
51
Congrats! You guessed the number 51
Number of trails: 6Explanation:
- 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/trailsmade by user before guessing the number correctly
printf("Guess the number\n"); scanf("%d",&guess);
Taking guess number from user.do { } while(flag);
Used do While because the loop should execute atleast once and has to be executed until user guess it correctly.- 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); }
- 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 numberagain 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 partwon'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.
- Iteration 1:
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.
No comments:
Post a Comment