×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous 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.

Generating random number

To generate a random number, use the srand() function and initialize it with the current time, and then use the rand() method along with the maximum_value by using the modulo (%) operator. It will generate a random number between 0 and the maximum_value - 1.

Header files

The following header must be included in order to use srand(), rand(), and time() functions:

  • stdlib.h: This header file contains the definition of srand() and rand() functions.
  • time.h: This header file contains the definition of time() function and other time related operations.

Code snippet to generate random number

Use the following code statements to generate a random number in C:

// 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;

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 !!!

C Advance Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.