Search This Blog

#Day60 #100DaysChallenge- Matlab Loops| Sum of digit in the given number

#Day60-Sum of digit in the given number
Task:

Write a code to  sum of  digit in the given number.
1234
SUM:10


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_d=sumofdigit(n)
sum_d=0;
while n~=0
    sum_d=sum_d+(floor(mod(n,10)));
    n=floor(n/10);
end

 Sample Input and Output

>> sum_d=sumofdigit(1234)

sum_d =

    10

>> sum_d=sumofdigit(12345)

sum_d =

    15

>> sum_d=sumofdigit(123457)

sum_d =

    22

>> sum_d=sumofdigit(1234578)

sum_d =

    30

Click here for Video Description



Free Codes: youtube.com/castorclasses


Live Support: t.me/matlabirawen


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); })();