Use a while loop to determine how many terms in the series 2
Use a while loop to determine how many terms in the series 2k(k+1), k=1, 2, 3, are required for the sum of the terms to exceed 5000. What is the sum for this number of terms? Use fprintf to display the number of terms with the corresponding sum.
Solution
k=1;
sum=0;
while(true)
sum += 2*k*(k+1);
if(sum>5000)
break;
end
k += 1;
end
fprintf(\'Number of terms in the sequence is %d, and the sum of them is %d\ \',k,sum);
