GoHacking.com: Ethical Hacking and Cyber Security
Home » C SOURCE CODES

Guessing Game In C

Submitted by on Saturday, 1 March 20088 Comments

This is a small guessing game written in C. In this guessing game you have to guess a number between 0 & 100. You have 8 chances to do that. Every time you guess wrongly the program will give you a hint that your guess is too high or your guess is too low. Based on this hint you have to guess the number in the remaining attempts. Here’s the code
 

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

void main()
{
int num,guess=-1,tries=0,pass=0;
time_t t;
srand((unsigned)time(&t));
num=rand()%100;
while((guess!=num)&&tries<8)
{
printf(“Enter the guess num b/w 0 & 100 (you have %d tries left out)\n”,(8-tries)); scanf(“%d”,&guess);
tries++;
if(guess==num)
{
printf(“Hurray you guessed it correctly!!!\n”);
pass=1;
}
else if(num< guess)
printf(“Your guess is too high\n”);
else
printf(“Your guess is too low\n”);
}
if(pass==0)
printf(“Sorry you lost! The correct number is %d\n”,num);
}

Popularity: 1% [?]


Subscribe to GoHacking.Com


Enjoyed this article?
Subscribe to GoHacking.Com and get daily updates in your email for free


  By using/following this site you agree to our Legal Disclaimer

8 Comments »

  • Chris said:

    THis actually worked. I have been working on this project for a simple game program and could not figure out what i was doing wrong. I am using Miracle C compiler and thought it was the compiler but soem of the code ideas here helped me to identify my issues. Thanks

  • hitesh said:

    that’s good

  • Krzystylez said:

    yea but what about for complete beginners, im guessing past in ntoepad save as “blabla.c”? or .exe i have no idea?!? help me!

  • Tarun Jha said:

    hi srikant u r superb.

    this really helps me a lot.

  • >>$h@nky said:

    hi srikanth its really superb……..

    i am inspired by you that now i’m intresting to knw abt hack something blabla….. till now i have no idea to study abt anything
    but after visiting ur site,it inspire for getting to know abt something abt hack ………. thanks u re really well…………..

    your guidance is useful for everyone……thanks hats off……

  • Msemakwelly said:

    hi Srikanth !! u r the undisputed man !! but am real a beginer and i wanna know !! so should i
    copy this copy this source code into the note pad and the wat follows…please!! help me

  • Aqib said:

    Hy Bro….
    I want to install Borland C
    in 64 Bit Processor
    Is it possible???
    Pls rply me on aqib160492@gmail.com

  • Mahalia Harian said:

    This site is mostly a stroll-by means of for all of the information you needed about this and didn?t know who to ask. Glimpse right here, and you?ll definitely discover it.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.