%
% A04.m
%
% Calculate the sum of integers from 1 to N using a for loop
%
% Programming Concepts: use of a for loop
%
% INPUT: User is prompted to enter the number of terms in the sum, N
% OUTPUT: The sum is displayed to the screen
%
% Matlab-Monkey.com
% 12/30/2014
% tidy-up your workspace
clc % clear command window
clear % clear all variables
close all % close all graphics windows
% get info from user
N = input('Enter number of terms in sum: ');
% initialize sum to zero
s = 0;
% Use a for loop to add numbers from 1 to N
for n=1:N
s = s + n;
end
% display result to command window
% notice that because the variables N and s are integers, we use the %i
% formatting in the fprintf statement
fprintf('\n')
fprintf('Sum of numbers from 1 to %i = %i \n',N,s)