Q:

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

0

 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. Do this 

using a for loop. Calling the function will look like this:

>> sumsteps2(11)

ans =

 36

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now