How to resolve the algorithm Sum digits of an integer step by step in the Oberon-2 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 Oberon-2 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 Oberon-2 programming language
Source code in the oberon-2 programming language
MODULE SumDigits;
IMPORT Out;
PROCEDURE Sum(n: LONGINT;base: INTEGER): LONGINT;
VAR
sum: LONGINT;
BEGIN
sum := 0;
WHILE (n > 0) DO
INC(sum,(n MOD base));
n := n DIV base
END;
RETURN sum
END Sum;
BEGIN
Out.String("1 : ");Out.LongInt(Sum(1,10),10);Out.Ln;
Out.String("1234 : ");Out.LongInt(Sum(1234,10),10);Out.Ln;
Out.String("0FEH : ");Out.LongInt(Sum(0FEH,16),10);Out.Ln;
Out.String("OF0EH : ");Out.LongInt(Sum(0F0EH,16),10);Out.Ln
END SumDigits.
You may also check:How to resolve the algorithm Gauss-Jordan matrix inversion step by step in the 11l programming language
You may also check:How to resolve the algorithm Variadic function step by step in the Klingphix programming language
You may also check:How to resolve the algorithm Hostname step by step in the Pascal programming language
You may also check:How to resolve the algorithm Singly-linked list/Traversal step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the Déjà Vu programming language