Sum of consecutive integers
some notes
% use while conecutive to compute sum of consecutive integers
clear;clc;
start = input('Enter the first value: ');
last = input('Enter the last value: ');
if start < last
i=1;
series=[start:1:last];
diff = last - start;
sum(i) = series(i);
while i<(diff+1)
i=i+1;
sum(i) = series(i-1)+series(i);
end
elseif start == last
sum = start;
else
i=1;
series = [start:-1:last];
diff = start -last;
sum(i) = series(i);
while i <(diff+1)
i=i+1;
sum(i) = series(i-1)+series(i);
end
end
disp(sum);
output
>>Enter the first value: 20
Enter the last value: 30
20 41 43 45 47 49 51 53 55 57 59
>>Enter the first value: 20
Enter the last value: 20
20
>>Enter the first value: 30
Enter the last value: 20
30 59 57 55 53 51 49 47 45 43 41