Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program that allows a user to guess a random number between 1 and 1000

ID: 3840630 • Letter: W

Question

Write a C program that allows a user to guess a random number between 1 and 1000. The function rand() from the header file can be used for this purpose. Before using the function, the random number generator must be provided a “seed” value to set it up. If the same seed value is supplied each time the program is run, the results of the random number generator will come out in the same order each time. A commonly used seed value is derived from the system clock, because the seconds count changes frequently enough to provide a different seed value each time we run the program. Include this in the beginning of your main program to seed the RNG: srand(time(NULL)); To use the time function, must be included. The rand() function returns a random integer between 0 and 32767. You will need to adjust the result to be between 1 and 1000 (think about how we might do this). Use a while loop to keep looping whenever the user guesses incorrectly. The loop should terminate when the user guesses the number correctly. Your program should count and print the number of guesses by the user. Each time the user guesses a number, you should print one of the following messages: Good guess. Too low, try again. Too high, try again. Sample Output: Guess my number 500 Too low, try again. Guesses: 1 750 Too high, try again. Guesses: 2 625 Too low, try again. Guesses: 3 675 Good guess. Guesses: 4

Explanation / Answer

import java.util.random;

import java.util.scanner;

public class test{

public static void main(string args[]){

random rand=new random();

int numbertogues=rand.nextint(1000);

int numberoftries=0;

scanner input=new

scanner(system.in);

int guess;

boolean win="false;

while(win==false)

{

system.out.println("guessing a number between 1 and 1000);

guess=input.nextint();

numberoftries++;

if (guess==numbertoguess){

win=true;

system.out.println("good guess");

}

else if(guess<numbertoguess)

system.out.println("too low,try again");

}else if(guess>numbertoguess)

system.out.println("too low,try again");

}

}

system.out.println("the number of guess was"+numbertoguess);

system.out.println("the number of tries"+tries);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote