The Computer Program we use is Matlab Q2 Partial sum of inte
The Computer Program we use is Matlab!
Q2. Partial sum of integers
Write a program using a FORLOOP to calculate the sum of integers up to N. Set the value of N at the beginning of the program. (N=100 was Gauss’s famous trick).
P(n)= k (here k=1 to n)
Please post a working code that was checked using MATLAB!
Solution
/**************partial sum of integer***********/
clear all
echo off all
N=100 % declaration of N and setting value as 100
sum=0 % sum variable for storing sum from 1 to N
%for loop for calculating sum of all integer
for x = 1:N %for loop start
sum=sum+x %storing sum into sum variable
end %for loop end
%printing value of sum
fprintf(\"Sum is = %d\ \",sum)
/***************output************/
If you have any query please feel free to ask.
Thanks a lot
