In: Computer Science
Write a program where a user of this program will play a game in which he/she needs to guess a target number, which is a number that the program has randomly picked in the range that the user chooses. The program will repeatedly prompt for the guessed number and provide a clue whether the guessed number is bigger or smaller than the target number, until the guessed number equals the target number.
// This is a comment: You did not mention a specific language so I created a simple on C programming language
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int smallInteger,largeInteger,guessInteger;
printf("Enter a number : ");
scanf("%d", &smallInteger);
printf("Enter a Larger number than before : ");
//loop to get input form the user and some error checking
scanf("%d", &largeInteger);
while( smallInteger > largeInteger){
printf("Please make sure the second number is neither smaller nor
the same as the first number \n");
printf("Enter a Larger number than before : ");
scanf("%d", &largeInteger);
}
srand(time(0)); //Using time as an random seed for our rand()
function
int originalInteger = (rand() % (largeInteger - smallInteger + 1))
+ smallInteger; // Bringing the range to our desired reange
//loop to compare the user input number to the actual
number
while(guessInteger!=originalInteger &&
guessInteger!="exit"){
printf("Alright, it's time to guess now. What do you think it is? :
\n");
scanf("%d", &guessInteger);
if(guessInteger != originalInteger ){
if(guessInteger < smallInteger || guessInteger >
largeInteger){
printf("Oops, you seem to have forgotten your range. It is between
%d and %d \n", smallInteger, largeInteger );
}
if(guessInteger < originalInteger ){
printf("Nope. Try again! Here is a hint : It is larger than you
expected \n");
}
if(guessInteger > originalInteger){
printf("Nope. Try again! Here is a hint : It is smaller than you
expected \n");
}
}else if( guessInteger == originalInteger){
printf ("OH, WINNER WINNER CHICKEN DINNER");
break;
}
}
return 0;
}
SAMPLE OUTPUT