game-guess a number between 1 and 100

 #include <stdio.h>

#include <stdlib.h>
#include <time.h>
int main()
{

    int i = 1, num, gnum;
    srand(time(0));
    num = rand() % 100 + 1;

    do
    {
        printf("Guess the number between 1 and 100:\n");
        scanf("%d", &gnum);
        if (gnum < num)
        {
            printf("Enter a higher number\n");
        }
        else if (gnum > num)
        {
            printf("Enter a lower number\n");
        }
        else
        {
            printf("You guessed the number in %d attempts", i);
        }

        i++;
    } while (num != gnum);

    return 0;
}

Comments

Popular posts from this blog

snake game Version-2 (using random number)

allocating dynamic memory using calloc

store a table in dynamic array and then change the size of array dynamically to store more numbers