Guessing Game in MATLAB using While Loop | MATLAB Tutorials

In this article, we will discuss about concept of IF ELSE statement and While Loop used in MATLAB. A simple guessing game is created with the help of IF Else and while loop. 

% Generate Guessing Game In MatlaB.
%generate a Random integer between 1 and 100.
clc;
clear all;
count=0;
a=randi([1,100]);
%Ask the user to guess the number.
fprintf('I''m thinking of a whole number between 1 and 100\n');
guess=input('Guess a number:');
while guess~=a
count=count+1;
if guess < a
% fprintf use to print the anything.
fprintf('The Guess was too low\n');
guess=input('Guess again > ');
else
fprintf('The Guess was too High\n');
guess=input('Guess again > ');
end
end
fprintf('Congratulations!You gueesed the corrected number in %g shots.\n ',count+1);
pause();

 

 

You may also like...

Leave a Reply