Search This Blog

#Day64 #100DaysChallenge- Matlab Loops| Generate and Sum the Series :3

#Day64- Generate and Sum the Series :3

Task:

Write a code to  find if the sum of series
Generate the series like this 9,99,999,9999,  with any number which user gives as input.
Note:Take user Input for number of digit.
Sum=402129

Note: This code can be done using the in-built command. But for the challenge, I am trying to avoid those.

Matlab code
function sum_series=sumofseries3(n,x)
b=1;
t=1;
tens=1;
sum_series=0;
while n>=1
   % sum_series(n)=(b*t)*x;
   sum_series=((b*t)+sum_series)*x;
    tens=tens*10;
    t=t+tens;
    n=n-1;
end
end

Sample Input and Output
>> sumofseries3(5,9)

ans =

      402129

>> sumofseries3(5,8)

ans =

      294648

>> sumofseries3(5,1)

ans =

       12345

>> sumofseries3(5,2)

ans =

       27762
Click here for Video description



Live Support: t.me/matlabirawen




No comments