Problem.
write a c program magic game the game gives a user one chance to choose a number which is known only by the system the system must display "hurray, you won $1000" IF THE NUMBER GUESSED BY THE USER IS CORRECT ELSE MUST DISPLAY " INCORRECT , BETTER LUCK NEXT TIME"?
Solution.
Code
#include <stdio.h>
#include <stdlib.h> // for rand()
#include <time.h> // for srand()
int main() {
int usrNum, sysNum;
srand(time(NULL)); // for different values of rand()
sysNum = rand() % 1000 + 1; // system number (1-1000 for better usability)
printf("Enter the positive integer number: ");
scanf("%d", &usrNum); // user number
if (usrNum == sysNum) {
printf("Hurray, you won $1000!");
} else {
printf("Incorrect, better luck next time!");
}
return 0;
}Result
Enter the positive integer number: 1000
Incorrect, better luck next time!
Process returned 0 (0x0) execution time : 1.822 s
Press any key to continue.
Comments