Write a function sumsteps2 that calculates and returns the sum of 1 to n in steps of 2, where n is an argument passed to the function. For example, if 11 is passed, it will return 1 + 3 + 5 + 7 + 9 + 11
belongs to book: MATLAB: A Practical Introduction to Programming and Problem Solving|Stormy Attaway|Fourth Edition| Chapter number:5| Question number:5.5
All Answers
total answers (1)
sumsteps2.m
function outsum = sumsteps2(n)
% sum from 1 to n in steps of 2
% Format of call: sumsteps2(n)
% Returns 1 + 3 + ... + n
outsum = 0;
for i = 1:2:n
outsum = outsum + i;
end
end
need an explanation for this answer? contact us directly to get an explanation for this answer