11. Write a program to compute the sum of the series 12 + 22 + 32. . . ., such that the sum is doesnot exceed 1000. The program should display how many terms are used in the sum.
{3 marks}
matlab only
1 to power of 2 , 2 to the power of 2 , 3 to the power of 3 , not 12 + 22 + 32
The MATLAB script to calculate and print the number of terms in the series whose sum is less than 1000 is:
%Declaring a int variable result to store sum of series
result=0;
% Declaring int variable count to keep track of no.of terms
count=0;
% Using while loop to calculate sum of series
% 1^1+2^2+3^3 ... till sum is less than 1000
while result<1000
% Incrementing count variable every time this loop runs
count=count+ 1;
% Calculating sum of series by adding new term to old sum
result = result + count.^count;
end
% Printing output using formatted print statement fprintf()
fprintf("Number of terms used in series : %d",count-1);
Using a while loop I have iterated through the sum of series 1^1+2^2+.... by checking if the sum is less than 1000. If the sum is greater than 1000 then we terminate the loop and print number of terms in the series made the highest sum of the series which is less than 1000.
I have tested the code and validated output. 1^1+2^2+3^3+4^4 results the sum 288. But when next term in the series 5^5 is added the result becomes 3413. So the number of terms in the series that make the highest sum less than 1000 is 4 and the sum is 288. I am sharing output screenshot for your reference.
Hope this answer helps you.
If you find this answer helpful, Please upvote the answer.
Thank you :)
Get Answers For Free
Most questions answered within 1 hours.