 |
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;
}
After copying this code at Terminal which is down on left side you need to write gcc game.c
I have write game.c because I have saved file name as game.c you write whatever you like
The game will ask you first "Guess the number between 1 to 100"
then you have to type any number between
if you typed higher num then it will display lower num, please
and if you typed lower num then it will display higher num, please.
This is the code for our Number Guessing Game.
If you have any doubt then you can ask in the comment section.
And Follow us by email and get notifications for free machine learning projects and codes.