how do i create a recursive function that gives the number of digits in an integer? i.e. does the same that length function does in MATLAB

1 回表示 (過去 30 日間)
function r = length_of(n)
end
it seems to be easy but right now i'm stuck with it

採用された回答

Cedric
Cedric 2013 年 3 月 30 日
編集済み: Cedric 2013 年 3 月 30 日
As Wayne mentioned, recursivity in unnecessary, but if you had to implement a recursive function, you could have built something like:
function r = length_of(n)
if n < 10
r = 1 ;
else
r = 1 + length_of(n/10) ;
end
end

その他の回答 (1 件)

Wayne King
Wayne King 2013 年 3 月 30 日
Why recursive?
A = 12;
B = 1000;
length(num2str(A))
length(num2str(B))
  1 件のコメント
Zaza
Zaza 2013 年 3 月 30 日
編集済み: Zaza 2013 年 3 月 30 日
i had a test and that was the requirement
anyway thanks

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by