Guessing Game in C
Bichunknown
c_cpp
a year ago
1.6 kB
1
Indexable
Never
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> int main() { // Write C code here char name[20]; int beginAnswer; int randomNum; int userInput; bool timesTried = 0; bool won = false; bool finishGame = false; printf("Welcome to Guess me challenge! \n"); printf("Please enter your name \n"); scanf(" %s",name); printf("Hello %s \n",name); printf("Do u want to play the game? \n"); printf("Enter 1 For Yes \n"); printf("Enter 2 For No \n"); scanf("%d",&beginAnswer); if(beginAnswer == 1){ randomNum = rand() % 20 + 1; printf("Please enter your guess between 1 and 20 \n"); scanf("%d",&userInput); while(!finishGame){ timesTried++; if(timesTried<=5){ if(userInput == randomNum){ won = true; finishGame = true; }else if(userInput > randomNum){ printf("Guess the number lower :D \n"); scanf("%d",&userInput); }else{ printf("Guess the number higher :D \n"); scanf("%d",&userInput); } }else{ finishGame = true; } } if(won){ printf("Congratulations! You Guessed the Correct Number"); }else{ printf("Game Over! You Ran out of tries \n"); printf("The number was : %d ", randomNum); } }else{ exit(0); } return 0; }