C Programming: Number Guessing Game

·

1 min read

Table of contents

No heading

No headings in the article.

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

int main(){
    int x,num;
    srand(time(0));
    num = rand()%20+1;
    int guess_count = 1;
    int guess_limit = 3;

    printf("WELCOME TO NUMBER GUESSING GAME :) \n");
    printf("Please Guess the number b/w 1 & 20. \n\n");
    printf("Enter Your Guess: ");
    scanf("%d", &x);

    while (guess_count < guess_limit){

        if(x < num){
            printf("Your Guess is Low. \n");
            printf("Enter Another Guess: ");
            scanf("%d", &x);}

        else if(x > num){
            printf("Your Guess is High. \n");
            printf("Enter Another Guess: ");
            scanf("%d", &x);}

        guess_count++;
    }

    if(x==num){
            printf("Congrats, Your Guess is Right. \n");
            printf("It is %d.", num);}

    else if(x!=num){
            printf("Sorry! You are out of Guessses. \n");
            printf("Correct number is %d.",num);}


    return 0;
}