C Exercises: Generate a random number
C Variable Type : Exercise-5 with Solution
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 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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming-exercises/variable-type/c-variable-type-exercises-5.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics