C Tutorial -4 Guess the number
Bhuwan Sharma
1 min read
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
srand(time(0));
int rand_num = rand()% 100 + 1;
printf("Guess the number sir!\n");
int user;
scanf("%d", &user);
int attempts = 1;
while(user != rand_num){
if(user>rand_num){
printf("Lower number please!\n");
scanf("%d", &user);
}
else if(user<rand_num){
printf("Higher number please!\n");
scanf("%d", &user);}
else if(user == rand_num){
break;
}
attempts++;
}
printf("You guessed the number in %d attempts: ", attempts);
}
0
Subscribe to my newsletter
Read articles from Bhuwan Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
C codeguess the number gamec programmingCProgramming BlogsProgramming TipslanguagesgameC Programming, Coding, Programming, Control Flow, Conditional Statements, if Statement, if-else Statement, else-if Ladder, switch Statement, Decision Making, Beginners, Learning C, Coding Tips, Code Examples, Coding Practice, Coding Challenges, C Language, Programming Basics, Coding Education, Code Development, Software Development, C Programming Concepts, Code Debugging, Algorithm, Logic, Coding Standards, Coding Community, C Coding Guidelines, C Programming Practices, C Coding Style
Written by