How to use for loop
Show older comments
Hello, I'm supposed to make a code that prompts the user to enter a positive real number to 1 decimal place. I should prompt the user 10 times. This was the code I used.
user_input = fprintf('%.1f', input('Enter a positive real number'));
for i = 1:10
if user_input <= 0
user_input = fprintf('%.1f', input('Enter a positive real number'));
end
end
Accepted Answer
More Answers (1)
KSSV
on 11 Jan 2021
user_input = fprintf('%.1f\n', input('Enter a positive real number'));
count = 0 ;
while count <= 10
if user_input >= 0
count = count+1 ;
user_input = fprintf('%.1f\n', input('Enter a positive real number'));
end
end
1 Comment
Inifome Eleluwor
on 11 Jan 2021
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!