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

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();