Search This Blog

#Day50 #100DaysChallenge- Matlab Loops|Sum of Digits in given number

#Day50-Sum of Digits in Given Number
Task:

Write a code to find sum of digits in given number. 
12345
Sum of Digits:15

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_value=sum_digits(n)

sum_value=0;
while n~=0
    sum_value=sum_value+mod(n,10);
    n=floor(n/10);
end

Sample Input and Output
>> sum_digits(12345679)


ans =

    37

>> sum_digits(8291)

ans =

    20

>> sum_digits(82917)

ans =

    27

>> sum_value=sum_digits(83917)

sum_value =

    28

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