Write a Matlab/Excel program that will play a guessing game. Have the computer generate a random whole number between 1 and 100. You can do this through the rand command. The user will guess a number and the computer will tell them if they are too high or too low. The program should stop if a correct guess if given. It should also display how many guesses were needed.
Expert Answer
% program that generates a random numbers between 1 and 100
% user will be given 3 chances to guess it.
nr = randi([1,100]);
i = 0;
choice = input(‘Guess the number(you will be having 3 chances so pick the correct one): ‘);
while choice ~= nr
i = i+1;
if i<3
% choice = input(sprintf(‘You are having only %d more guesses! ‘, i));
else
fprintf (‘Its WRONG buddy please try again! The number was %dn’, nr)
break
end
end
if choice == nr
fprintf(‘Congratulations, you guess was correct and the number is %d!n’, nr)
end
Sample Output :
Guess the number(you will be having 3 chances so pick the correct one): 25
Congratulations, you guess was correct and the number is 25