Page 1 of 1

How to sum digits of a number

Posted: 07 Jan 2021 15:28
by gkick17
Hi all,

did this a long time ago but have forgotten.
If I have a number eg 7292 and I want to basically add all the digits to generate the result of 20, how can I accomplish this ?
Thanks for your thoughts

Re: How to sum digits of a number

Posted: 07 Jan 2021 22:18
by Squashman

Code: Select all

@echo off
setlocal
SET "number=7292"
set "sum=0"
for /F "delims=" %%G IN ('cmd /u /c "echo %number%"^|find /V ""') do (
    set /A sum+=%%G
)
echo SUM IS: %sum%
endlocal
pause
Output

Code: Select all

C:\Users\Squashman\Desktop>sum.bat
SUM IS: 20
Press any key to continue . . .

Re: How to sum digits of a number

Posted: 07 Jan 2021 22:27
by gkick17
Thank you, very much appreciated ! Working like a treat