Tuesday 30th September 2008
 
 
 
 
 
 


 
 
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

[C] Magic Number

November 16, 2006 Author: Chris T Posted in: Code Tutorials
 

A simple program in c which the program choose one random number from 1-500 and the user has to guess that number. The program notifies user if his guess is too low or too hight and counts all the user tries.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int main(void) {     
    int magic, guess, counter;
    srand(time(NULL));
 
    magic=(rand()%500)+1;
    counter=0;
 
    do { printf ("Make Your Guess: ");
 
   scanf ( "%d" , &guess);   
         counter++;                       
 
         if ( guess==magic ) {
              printf ( "You have found it after %d trial(s) , " , counter);
              printf ( "%d is the number ! \n" , magic );
              }
         else {
               printf ( "Wrong number , You are ");
               if ( guess > magic)  printf ("Too high... ");
               else  printf ("Too low... ");
               printf ( "You have made %d trial(s) \n" , counter);
               }         
}while (guess!=magic);                         
 
system("PAUSE");
return 0;   
}
 
Views: 2,411
 

Feed for this Entry Trackback Address The permalink
 

 

Be the first to comment this entry.

 

 

Leave a Comment