I want to write a recursive Function that can calculate the sum of all the digit. If the input is 12345 then the answer will be 1+2+3+4+5 , without using string2num and loop.

73 ビュー (過去 30 日間)
function output= digit_sum(n)
if n<10
output=fix(n);
end
if n>0
output=output+digit_sum(n*0.1);
end
end
I wrote this code but the problem I am facing is if i set output=0; anywhere then in all the recalling function process my result will turn to be 0, How to Solve this ?
  4 件のコメント
James Tursa
James Tursa 2020 年 8 月 17 日
編集済み: James Tursa 2020 年 8 月 17 日
Maybe you should take a step back and write down the algorithm on paper using words instead of code. Then take a simple 2 digit example and run it through your algorithm on paper. Once you have things figured out for your algorithm, then turn it into code and start testing. That will force you to understand the algorithm first, before you even get to the coding stage.
Asif Ahmed
Asif Ahmed 2020 年 8 月 17 日
編集済み: Asif Ahmed 2020 年 8 月 17 日
Thanks for your help

サインインしてコメントする。

採用された回答

David Hill
David Hill 2020 年 8 月 17 日
function output= digit_sum(n,a)
if nargin==1
a=0;
end
if n<10
output=a+fix(n);
else
output=digit_sum(floor(n*.1),a+mod(n,10));
end
end
  3 件のコメント
David Hill
David Hill 2020 年 8 月 17 日
Using nargin, your function can have 1 or more inputs.
output = digit_sum(12345);
Try the above on my function.
Asif Ahmed
Asif Ahmed 2020 年 8 月 17 日
編集済み: Asif Ahmed 2020 年 8 月 17 日
function output= digit_sum(n)
if n<10
output=fix(n);
end
if n>0
output=((n/10 -fix(n/10))*10)+digit_sum(fix(n*0.1));
end
end
Thanks , I got the problem where i was mistaking,Now the code runs fine. Thanks for your help.
function output= digit_sum(n)
if n<10
output=fix(n);
end
if n>0
output=mod(n,10)+digit_sum(fix(n*0.1));
end
end

サインインしてコメントする。

その他の回答 (7 件)

Saptarshi Neogi
Saptarshi Neogi 2020 年 8 月 30 日
You can do this with any inbuit functions.
function x = digit_sum(n)
x=0;
if n>0
x=mod(n,10)+digit_sum(floor(n./10));%recursive
end

Sanjay Raju
Sanjay Raju 2020 年 9 月 13 日
function x=digit_sum(inp)
if inp<10
x=inp;
else
x = mod(inp,10) + digit_sum(fix(inp/10));
end
end

Farhath Surayya
Farhath Surayya 2020 年 11 月 19 日
function output= digit_sum(n) if n<10 output=fix(n); end if n>0 output=output+digit_sum(n*0.1); end end

Ramesh Patel
Ramesh Patel 2021 年 7 月 22 日
function sum = digit_sum(num)
if fix(num)==0
sum=0;
else
x = fix(num/10);
sum = rem(num,10) + digit_sum(x);
end
end
%is this code correct.output is right but i am not able to understand when
%if fix(num)==0 condition will true than value of sum should be zero. but
% zero is not output ,why?

Ans sadiq
Ans sadiq 2021 年 8 月 19 日
function out=digit_sum(in)
q=in;
a=q/10;
b=floor(a);
c=q-b*10;
w=c;
if q>0
w=w+digit_sum(b);
end
out=w;
end

Selman Baysal
Selman Baysal 2022 年 1 月 5 日
Hi, i have just done this homework. Here is my code:
function output = digit_sum(A)
remA = rem(A,10); % calculates the last digit of A
if isequal(A,0)
output = 0;
else
A = (A-rem(A,10))/10; % gives the new A with substracting the last digit
output = remA + digit_sum(A);
end
end

youssef
youssef 2023 年 10 月 25 日
function x=digit_sum(n)
x=0;
if n<=0
x=0;
else
x=x+mod(n,10)+digit_sum((n-mod(n,10))/10);
end
end

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by