MATLAB: How to convert this to a for loop for loop x = input('Enter a number between 1 and 100: '); i = 0; while x>0 x=x-i; i=i+1; end fprintf('The while loop ran for %d interations\n' ,i) Best Answer % your while loopx = input('Enter a number between 1 and 100: ');i = 0;while x>0 x=x-i; i=i+1;end fprintf('The while loop ran for %d interations\n' ,i)% my for loopx = input('Enter a number between 1 and 100: ');for i = 0:100 x = x - i; if x <= 0 break; endend fprintf('The while loop ran for %d interations\n', i + 1)Enter a number between 1 and 100: 30The while loop ran for 9 interationsEnter a number between 1 and 100: 30The while loop ran for 9 interations Related SolutionsMATLAB: How do you finish this for loop? It is telling me to define the variable “total”, but what do I put in order for it to flow with the loop total=0; % you forgot to intialise total ( to define total before usage)for i = 1:5 number = input('Enter a number: '); total = total + number;fprintf('The sum of the numbers is %.2f\n', total);end MATLAB: How to convert from a while loop to a for loop for i=0:11:109 fprintf('i = %d\n',i)end Related QuestionWrite a function to ask for given number and report whether they are between 10 and 100 or notWhile loop: value increases, break if not.
Best Answer