August 20, 2009

Guessing game (c program code)

#include
int main ()
{
int n;
const int guess=10;
int flag=0;
printf("Guess the number that i am thinking\n");
scanf("%d",&n);
if (!(n>=guess))
printf("Your number is too low. try again\n");
else if (n>guess)
printf("Your number is too high. Try again\n");
else
flag=1;
if (flag==0)
scanf("%d",&n);
if (!(n>=guess))
printf("Your number is too low. try again\n");
else if (n>guess)
printf("Your number is too high. Try again\n");
else
flag=1;

if (flag==0)
scanf("%d",&n);
if (!(n>=guess))
printf("Your number is too low. try again\n");
else if (n>guess)
printf("Your number is too high. Try again\n");
else
flag=1;

if (flag==0)
scanf("%d",&n);
if (!(n>=guess))
printf("Your number is too low. try again\n");
else if (n>guess)
printf("Your number is too high. Try again\n");
else
flag=1;

if (flag==0)
scanf("%d",&n);
if (!(n>=guess))
printf("Your number is too low. try again\n");
else if (n>guess)
printf("Your number is too high. Try again\n");
else
flag=1;



if (flag==1)
printf("Congrats\n");
else
printf("im sorry!");
return 0;
}

This a simple guessing game program that was written using Quincy. Instead of using random function I intialized the guess to 10 wherein the number to be guessed is 10. and rather using a loop I used the if and else statements. The user is given 5 tries to guess the number. After 5 tries the program will display the "im sorry!". NOTE: I have change the expression statements in the if...

RESULTS


Start of the program...






*The program displays "Congrats" after successfully guessing the number.



*The program still continues to give the user a chance to guess the number.



*The program terminates after five tries had been given to the user.. and displays the "im sorry" message..




No comments:

Post a Comment