Guess a random number with nested if...else statement - C Data Type

C examples for Data Type:Random Number

Description

Guess a random number with nested if...else statement

Demo Code

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

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

   magic = rand(); /* get a random number */

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

   if (guess == magic) {
      printf("** Right **");
      printf(" %d is the magic number\n", magic);
   }//from www .j  av  a  2s  .c  o  m
   else {
      printf("Wrong, ");
      if (guess > magic) printf("too high\n"); /* nested if */
      else printf("too low\n");
   }

   return 0;
}

Result


Related Tutorials