How to Add the Individual digits of a result ?

2 ビュー (過去 30 日間)
Shravankumar P
Shravankumar P 2014 年 6 月 28 日
編集済み: Star Strider 2014 年 6 月 28 日
>> 789*4567
ans =
3603363
I want to add each element of the ans :
i.e., >> 3+6+0+3+3+6+3
ans =
24
Help me how to do this?

採用された回答

dpb
dpb 2014 年 6 月 28 日
Lots of ways...just a couple--
>> sum(str2num(num2str(789*4567).'))
ans =
24
>> >> sum(sscanf(num2str(789*4567),'%1d'))
ans =
24
>>

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 6 月 28 日
Try this:
theNumber = 3603363
theString = num2str(theNumber)
theSum = sum(theString - '0')
(I hope I didn't just do your homework.)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by