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 CenterFile Exchange에서 Crystals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by