#Day60 #100DaysChallenge- Matlab Loops| Sum of digit in the given number
#Day60-Sum of digit in the given number
Task:
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
Sample Input and Output
Click here for Video Description
Free Codes: youtube.com/castorclasses
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
>> 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
No comments