w3resource

C Exercises: Generate a random number


5. Random Number Generation

Write a C program to generate a random number.

Sample Solution:

C Code:

#include<stdio.h>     // Include the standard input/output header file.
#include<stdlib.h>    // Include the standard library header file.
#include<time.h>      // Include the time header file for generating random numbers.

int main ()           // Start of the main function.
{
int number, input;   // Declare two integer variables 'number' and 'input'.

srand ( time(NULL) );   // Initialize the random seed using the current time.

number = rand() % 10 + 1;   // Generate a random number between 1 and 10 and store it in 'number'.

do {   // Start of a do-while loop.
printf ("\nGuess the number (1 to 10): ");   // Print a message prompting the user to guess the number.
scanf ("%d",&input);    // Read the user's input and store it in 'input'.

if (number > input)   // If the random number is greater than the user's input.
printf ("The number is higher\n");   // Print a message indicating that the number is higher.

    } while (number!=input);   // Continue looping as long as the user's input is not equal to the random number.

printf ("That is correct!\n\n");   // Print a message indicating that the user guessed correctly.

return 0;   // Return 0 to indicate successful execution of the program.
}   // End of the main function.
	

Sample Output:

Guess the number (1 to 10): 6                                                                                 
The number is higher 

Flowchart:

C Exercises Flowchart: Generate a random number

For more Practice: Solve these Related Problems:

  • Write a C program to generate a random integer between 1 and 100 using rand() seeded with the current time.
  • Write a C program to generate a random floating-point number between 0 and 1 and display it with precision formatting.
  • Write a C program to generate an array of random numbers and then sort them using a built-in sorting function.
  • Write a C program to implement a custom pseudo-random number generator using a linear congruential method.

C Programming Code Editor:



Previous: Write a C program to convert a string to a double.
Next: Write a C program to sort the elements of an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.