nguess.c (799B)
1 /* Exercise 5.35 */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main() 7 { 8 int num, gnum; 9 10 while(gnum = 1 + rand() % 1000) { 11 printf("\nI have a number between 1 and 1000.\n"); 12 printf("Can you guess my number?\n"); 13 printf("Please type your first guess.\n"); 14 scanf("%d", &num); 15 16 while(num != gnum) { 17 if(num < gnum) 18 printf("Too low. Try again.\n"); 19 else 20 printf("Too high. Try again.\n"); 21 22 scanf("%d", &num); /* try again */ 23 } /* end while */ 24 25 printf("\nExcellent! you guessed the number!\n"); 26 printf("Would you like to play again (y or n)?\n"); 27 28 getchar(); /* take newline and, of course, ignore it */ 29 30 if(getchar() == 'n') 31 break; 32 } /* end while */ 33 34 return 0; 35 } /* E0F main */ 36