How to Write a Program of Number Guessing Game in C


In this Blog I am going to show, How you can write simple code of game in C language.
The game which we are coding is super easy to make, the game name is Number Guessing Game.


// Number Guessing game

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

int main()
{
    int number, guess, nguesses = 1;
    srand(time(0));
    number = rand() % 100 + 1;
    // printf("The number is %d\n", number); 
    // This is a number which you have to guess

    do
    {
        printf("Guess the number between 1 and 100\n");
        scanf("%d", &guess);

        // The condition to check the number you have guessed was higher or lower than real number.
        if (guess > number)
        {
            printf("Lower number please\n");
        }
        else if (guess < number)
        {
            printf("Higher number please\n");
        }
        else
        {
            printf("You guessed it in %d attempts\n", nguesses);
        }
        nguesses++;

    } while (guess != number); // the loop will run until this condition became true

    return 0;
}

Review All Practicals:-

https://getyourcodehere.blogspot.com/p/gtu-programming-for-problem-solving.html?m=1

Post a Comment

0 Comments