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

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

Popular Posts