How to resolve the algorithm Sum digits of an integer step by step in the Forth programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sum digits of an integer step by step in the Forth programming language

Table of Contents

Problem Statement

Take a   Natural Number   in a given base and return the sum of its digits:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sum digits of an integer step by step in the Forth programming language

Source code in the forth programming language

: sum_int 0 begin over while swap base @ /mod swap rot + repeat nip ;

 2 base ! 11110 sum_int decimal  . cr
10 base ! 12345 sum_int decimal  . cr
16 base ! f0e   sum_int decimal  . cr


  

You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Wren programming language
You may also check:How to resolve the algorithm Sphenic numbers step by step in the Sidef programming language
You may also check:How to resolve the algorithm Last Friday of each month step by step in the Pascal programming language
You may also check:How to resolve the algorithm Machine code step by step in the Raku programming language
You may also check:How to resolve the algorithm Stack step by step in the Z80 Assembly programming language