Guess a random number with if statement - C Data Type

C examples for Data Type:Random Number

Introduction

The following program contains an example of if.

It generates the magic number using the standard random number generator rand( ), which returns an arbitrary number between 0 and RAND_MAX.

RAND_MAX defines an integer value that is 32,767 or larger.

The rand( ) function requires the header <stdlib.h> .

Demo Code

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

int main(void)
{
   int magic; /* magic number */
   int guess; /* user's guess */

   magic = rand(); /* generate the magic number */

   printf("Guess the magic number: ");
   scanf("%d", &guess);

   if (guess == magic) printf("** Right **");

   return 0;//from  w ww  .  j  a  v a2 s  .  c  o  m
}

Result


Related Tutorials