display while loop output as an array
Show older comments
how do you display the output of a while loop as an array.
code:
function [] = hailstone_sequence(n)
n = input('Value for n: ');
h = 1;
while(n~=1)
if n==1
return
elseif mod(n,2)
n=3*n +1
else
n=n/2
end
h=h+1;
end
Accepted Answer
More Answers (1)
jean claude
on 5 Oct 2017
Edited: jean claude
on 5 Oct 2017
function [output] = hailstone_sequence(n)
output=[n];
h = 1;
while(n~=1)
if n==1
return
elseif mod(n,2)
n=3*n +1
else
n=n/2
end
h=h+1;
output= [output n];
end
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!