% -----------------------------------------------------
% runningsum1toN.m
% -----------------------------------------------------
% Write a program to compute the sum of all integers from 1 to N (a user
% input). Use a for loop to perform this running sum operation.  
% -----------------------------------------------------
% Notes: This can also be done using vectors only.  This is left to the
% user as a practice problem. 
% -----------------------------------------------------
% Copyright and license details available at matlabmarina.com
%------------------------------------------------------

clear;
clc;

% Input N to compute the sum from 1 to N
N = input('Input a positive integer N  :  ');

% Initialization (else 1st iteration, rsum will be undefined)
rsum = 0; 

% for loop with counter k, k starts at 1,increments by 1 each iteration, ends at N.
% Compute the running sum
for k = 1 : 1 : N
    rsum = rsum + k; % add N times
end

% display final solution
fprintf('The sum of the series 1 to %d is %d\n', N, rsum); 
