Use only Matlab to write a program you can use for/while/case loop Number guessi
ID: 2074196 • Letter: U
Question
Use only Matlab to write a program
you can use for/while/case loop
Number guessing game. Write a program, guessing . m, that does the following: The computer generates a random integer between 1 and 20 (inclusive). The computer displays "I am thinking of a number between 1 and 20." It prompts the user to input a guess. While the guess is not equal to the computer's number, it tells the user whether the guess was too low or too high, and prompts for another guess. When the user guesses the right number, the computer congratulates the user and ends the game. (Hint: Use a while loop.) A run of the program (for which the user guessed 10 and 15 before correctly guessing 13) might look something like this: >>guessing I am thinking of a number between 1 and 20 Enter your guess: 10 Too 1ow Enter your guess: 15 Too high Enter your guess: 13 Right!Explanation / Answer
The code you asked for is here:
clear all;
clc;
disp('I am thinking of a number between 1 and 20')
a = randi(20);
b = input('Enter your guess: ');
while (a~=b)
if a>b
disp('Too low')
elseif a<b
disp('Too high')
end
b = input('Enter your guess: ');
end
disp('Right!')
Just copy it and paste it in a file called guessing.m and then save and run. You will get what you want.
I hope it was easy. Please rate.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.