0% found this document useful (0 votes)
283 views4 pages

Guessing Game C Program

This C program implements a number guessing game where a user tries to guess a randomly generated number between 1 and 100. The program provides feedback on each guess by telling the user if their guess is too high or low, and whether they are getting warmer or colder compared to the previous guess. It uses conditional statements to handle all possible guessing scenarios, and loops until the user correctly guesses the randomly selected number.

Uploaded by

Insane_Elmo
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
283 views4 pages

Guessing Game C Program

This C program implements a number guessing game where a user tries to guess a randomly generated number between 1 and 100. The program provides feedback on each guess by telling the user if their guess is too high or low, and whether they are getting warmer or colder compared to the previous guess. It uses conditional statements to handle all possible guessing scenarios, and loops until the user correctly guesses the randomly selected number.

Uploaded by

Insane_Elmo
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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

h>

int main(void) { int guess,check,my_num=32; //initializing variables, my_num is the number to be guessed, guess is the user guess, check is previously guessed value printf("Im thinking of a number between 1 and 100.\n"); printf("What number am I thinking of?"); fflush(stdout); scanf("%d",&guess); //asking user for input while ((guess<1)||(guess>100)){ //fool-proofing to keep guesses between 0-100 printf("Hey! Thats not between 1 and 100!\n"); printf("Ill pretend you didnt say that.\n"); printf("What number am I thinking of?\n"); fflush(stdout); scanf("%d",&guess);} check=guess; //check gets set to guess here, but guess will be changed before check is used so that check is the previous guess value if (guess==my_num){ //condition in case user guesses correctly on first try printf("Wow, you got it on your first try!"); return EXIT_SUCCESS; } else printf("Not bad for your first try.\n"); //else statement for the highly likely event that user guesses wrong on first try printf("What number am I thinking of?\n"); //asking user for a guess once more before going into the while loop

fflush(stdout); scanf("%d",&guess); while (guess!=my_num){ //while loop for all remaining guess attempts, loops until user guesses correctly if(((my_num-guess)<=1)&&((my_num-guess)>=-1)){ // if/else statements for every guessing case possible, this one is in case the guess is within 1 printf("Youre incredibly hot!\n");} else if((guess<my_num)&&((guesscheck)<0)&&(check>my_num)&&((my_num-guess)<(check-my_num))){ //cases for if the previous value was above/below and the current guess is below/above printf("Youre getting warmer ....\n");} else if((guess<my_num)&&((guesscheck)<0)&&(check>my_num)&&((my_num-guess)>(check-my_num))){ printf("Ouch! Youre getting colder.\n");} else if((guess>my_num)&&((guess-check)>0)&&(check<my_num)&&((guessmy_num)<(my_num-check))){ printf("Youre getting warmer ....\n");} else if((guess>my_num)&&((guess-check)>0)&&(check<my_num)&&((guessmy_num)>(my_num-check))){ printf("Ouch! Youre getting colder.\n");} else if((guess>my_num)&&((guess-check)>0)){ //cases for if both the current and previous guess are both above/below printf("Ouch! Youre getting colder.\n");} else if((guess<my_num)&&((guess-check)<0)){ printf("Ouch! Youre getting colder.\n");} else if((guess>my_num)&&((guess-check)<0)){ printf("Youre getting warmer ....\n");} else if((guess<my_num)&&((guess-check)>0)){

printf("Youre getting warmer ....\n");} check=guess; //resets check to the current guess, which will change right after printf("What number am I thinking of?\n"); //asks the user for another guess after each unsuccessful attempt fflush(stdout); scanf("%d",&guess); } printf("Thats amazing!\n");//statement for when the user guesses correctly printf("Good for you!\n"); /* Output * Im thinking of a number between 1 and 100. * What number am I thinking of? *0 * Hey! Thats not between 1 and 100! * Ill pretend you didnt say that. * What number am I thinking of? * 101 * Hey! Thats not between 1 and 100! * Ill pretend you didnt say that. * What number am I thinking of? * 50 * Not bad for your first try. * What number am I thinking of? * 40 * Youre getting warmer ....

* What number am I thinking of? * 60 * Ouch! Youre getting colder. * What number am I thinking of? * 30 * Youre getting warmer .... * What number am I thinking of? * 35 * Ouch! Youre getting colder. * What number am I thinking of? * 33 * Youre incredibly hot! * What number am I thinking of? * 31 * Youre incredibly hot! * What number am I thinking of? * 32 * Thats amazing! * Good for you! */ return EXIT_SUCCESS; }

You might also like