Generate Random Numbers - C Data Type

C examples for Data Type:Random Number

Description

Generate Random Numbers

Demo Code

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

int main()/*from   w  w  w  .j  ava 2  s .com*/
{
   int iRandomNum = 0;
   int iResponse = 0;

   iRandomNum = (rand() % 10) + 1;
   printf("\nGuess a number between 1 and 10: ");
   scanf("%d", &iResponse);
   if (iResponse == iRandomNum)
      printf("\nYou guessed right\n");
   else {
      printf("\nSorry, you guessed wrong\n");
      printf("The correct guess was %d\n", iRandomNum);
   }
}

Result


Related Tutorials