# C program to guess a random number

This program will read a random number and ask to user to guess it. This is just like a small game program in which user has to guess correct number which is generated randomly. Here program will give 7 attempts to guess the number, on each attempt program will inform that entered number is less than or greater than the random generated number so that user can easily guess that particular number.

## Guess random generator number using C program

```/*C program to guess a random number.*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int random_genNo=0,count=0,num;
int stime;
long ltime;

//initialise srand with current time, to get random number on every run
ltime = time(NULL);
stime = (unsigned) ltime/2;
srand(stime);

//generate random number
random_genNo=rand()%1000;

//run infinite loop
while(1)
{
//increase counter
count+=1;

printf("\n\nGuess a number from (0 to 1000): ");
scanf("%d",&num);

//compare entered number with generated number

if(random_genNo==num){
printf("Congratulations, you have guessed a correct number.");
break;
}
else if(random_genNo<num){
printf("Generated number is less than entered number, try your luck again...");
}
else if(random_genNo>num){
printf("Generated number is greater than entered number, try your luck again...");
}

if(count==7){
printf("\n\n### Maximum limit of atttempt finished, BAD LUCK !!!\n");
break;
}
}

return 0;
}
```

Output

```    First Run:
Guess a number from (0 to 1000): 700
Generated number is less than entered number, try your luck again...

Guess a number from (0 to 1000): 350
Generated number is less than entered number, try your luck again...

Guess a number from (0 to 1000): 150
Generated number is greater than entered number, try your luck again...

Guess a number from (0 to 1000): 210
Generated number is less than entered number, try your luck again...

Guess a number from (0 to 1000): 208
Generated number is less than entered number, try your luck again...

Guess a number from (0 to 1000): 207
Congratulations, you have guessed a correct number.

Second Run:
Guess a number from (0 to 1000): 900
Generated number is less than entered number, try your luck again...

Guess a number from (0 to 1000): 500
Generated number is less than entered number, try your luck again...

Guess a number from (0 to 1000): 400
Generated number is less than entered number, try your luck again...

Guess a number from (0 to 1000): 200
Generated number is greater than entered number, try your luck again...

Guess a number from (0 to 1000): 300
Generated number is greater than entered number, try your luck again...

Guess a number from (0 to 1000): 330
Generated number is greater than entered number, try your luck again...

Guess a number from (0 to 1000): 340
Generated number is greater than entered number, try your luck again...

### Maximum limit of atttempt finished, BAD LUCK !!!
```

What's New

Top Interview Coding Problems/Challenges!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates