Home » C solved programs » Advance C programs

# 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; //read number from user 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 !!!

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions