MATLAB: How to contain this in one “for” loop

for loopMATLAB

fprintf('Problem 2: \n')
for index = 1:1:1
n_value=input('Please, input an n value: \n');
n=1:1:5;
manipulate_n=1./(2.^n);
sum1=sum(manipulate_n);
fprintf('There are %f n values, the sum is %f, and the difference is %f \n',length(n),sum1,1-sum1)
end
for index = 1:1:1
n_value2=input('Please, input an n value: \n');
n2=1:1:10;
manipulate_n2=1./(2.^n2);
sum2=sum(manipulate_n2);
fprintf('There are %f n values, the sum is %f, and the difference is %f \n',length(n2),sum2,1-sum2)
end
for index = 1:1:1
n_value3=input('Please, input an n value: \n');
n3=1:1:40;
manipulate_n3=1./(2.^n3);
sum3=sum(manipulate_n3);
fprintf('There are %f n values, the sum is %f, and the difference is %f \n',length(n3),sum3,1-sum3)
end

Best Answer

for index = 1:1:1
n_value=input('Please, input an n value: \n');
n=1:1:5;
manipulate_n=1./(2.^n);
sum1=sum(manipulate_n);
fprintf('There are %f n values, the sum is %f, and the difference is %f \n',length(n),sum1,1-sum1)
n_value2=input('Please, input an n value: \n');
n2=1:1:10;
manipulate_n2=1./(2.^n2);
sum2=sum(manipulate_n2);
fprintf('There are %f n values, the sum is %f, and the difference is %f \n',length(n2),sum2,1-sum2)
n_value3=input('Please, input an n value: \n');
n3=1:1:40;
manipulate_n3=1./(2.^n3);
sum3=sum(manipulate_n3);
fprintf('There are %f n values, the sum is %f, and the difference is %f \n',length(n3),sum3,1-sum3)
end
That was real easy since the for loops only execute one time - basically there's no looping whatsoever.