C Example: Number Guessing Game

#include 
#include 

main() {
	printf ("Medo's Guessing Game\nGuess A Number Between 1-200\n");
	game();
	return 0;
}
game() {
	int input=0;
	int number;
	int tries=0;
	srand((unsigned)time(0));
	number=rand() % 200 + 1;
	while (input!=number){
		printf("guess a number:");
		scanf("%i",&input);
		tries = tries +1;
		if (input  number){
			printf("little bit lower\n");
		}
		if (input == number){
			printf ("Yay! you gussed it right in %d tries\n\n", tries);
			ask();
		}
	}
	return 0;
}

ask() {
	int yn;
	printf ("try again? 0/1: ");
	scanf("%d",&yn);
	if ( yn == 1 ){
		printf ("Guess the new number\n");
		game();
	}
	if ( yn !=1 ){
		printf ("Goodbye! \n\n");
		return 0;
	}
}
Advertisements

Leave a Reply :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s