212k views
1 vote
Write a C program that asks the user to guess a number between 1 and 15 (1 and 15 are included). The user is given three trials. Say the hidden number is 8, below are three possible sample runs:

1 Answer

1 vote

Answer:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main(void) {

srand(time(NULL));

int num = rand() % 15 + 1;

int guess_status = 0;

int current_guess;

int count = 0;

printf("Welccome to the guess a number game! \\");

printf("I'm thinking of a number between 1 and 15 (1 and 15 are included).\\");

printf("Guess the number: ");

do {

scanf("%d", &current_guess);

count++;

if (current_guess == num) {

printf("Your guess is correct, the number is %d\\", current_guess);

guess_status = 1;

}

else if(count == 3){

printf("Your guess is wrong, the number is %d\\", num);

break;

}

else if (current_guess < num) {

printf("Your guess is too low, try something higer: ");

}

else if (current_guess > num) {

printf("Your guess is too high, try something higer: ");

}

} while (guess_status == 0);

return 0;

}

User Marko Gresak
by
6.1k points