Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

#Day63 #100DaysChallenge- Matlab Loops| Generate and Sum the Series :2

#Day63- Generate and Sum the Series :2

Task:

Write a code to  find if the sum of series
Generate the series like this 1,11,111,1111,11111
Note:Take user Input for number of digit.
Sum=12345

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=sumofseries2(n)
b=1;
t=1;
tens=1;
sum_series=0;
while n>=1
    sum_series=(b*t)+sum_series;
    tens=tens*10;
    t=t+tens;
    n=n-1;
end
end

Sample Input and Output

>> sumofseries2(3)

ans =

   123

>> sumofseries2(5)

ans =

       12345

>> sumofseries2(9)

ans =


   123456789

Click here for Video Description



No comments

Popular Posts