C - Write program to guess a random number seeded by current time

Requirements

Write program to guess a random number seeded by current time.

A random number is generated to make the guessing game more interesting.

Demo

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

int main()//from ww  w . j  a  v a  2  s.com
{
    int r,guess;

    srand((unsigned)time(NULL));
    r=rand();
    printf("Can you guess the secret number: ");
    scanf("%d",&guess);
    if(guess==r)
    {
        puts("You guessed it!");
        return(0);
    }
    if(guess!=r)
    {
        puts("Wrong!");
        printf("The secret number was %d\n",r);
        return(1);
    }
}

Result

Related Exercise