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

(While loops)Use matlab to solve problems (1). Write a program that asks the use

ID: 669751 • Letter: #

Question

(While loops)Use matlab to solve problems

(1). Write a program that asks the user to enter an integer that is greater than 0. The function will keep on asking the user for the number until it is valid.
(2). Write a program that calculates the square root of a user-provided number until the user provides a negative number

While loops)Use matlab to solve). Write a program that asks the user to enter an integer that is greater than 0. The function will keep on asking the user for the number until it is valid. (2). Write a program that calculates the square root of a user-provided number until the user provides a negative number

Explanation / Answer

(1)

% MATLAB

%

% Using a while loop to ask the user to input a number

% between 1 and 10 (inclusive).

%

%   Variables:

%        value : variable to store the input

%

value = input ('Please Enter a Number between 1 and 10 (1-10)');

while ( value < 1 || value > 10)

fprintf('Incorrect input, please try again. ');

value = input ('Enter a Number between 1 and 10 (1-10)');

end % while

(2)

% MATLAB

%

n=input(’Enter the number now ’)

if n<0

disp(’This number is negative’)

else

disp(s’Square root is’)

disp(sqrt(n))

end