Number Guessing Game |
We will make an interesting Game to understand C Language properly and to motivate us to code C language
In this Interesting Game, we have to Guess the Correct number
We have used loops in code
we will use clion to Code our Game
Clion |
This is the code given below for the Number Guessing Game
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int number, guess, nguesses=1;
srand(time(0));
number = rand()%100 + 1; // Generates a random number between 1 and 100
// printf("The number is %d\n", number);
// Keep running the loop until the number is guessed
do{
printf("Guess the number between 1 to 100\n");
scanf("%d", &guess);
if(guess>number){
printf("Lower number please!\n");
}
else if(guess<number){
printf("Higher number please!\n");
}
else{
printf("You guessed it in %d attempts\n", nguesses);
}
nguesses++;
}while(guess!=number);
return 0;
}
0 comments:
Post a Comment