How to resolve the algorithm Harshad or Niven series step by step in the PL/M programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Harshad or Niven series step by step in the PL/M programming language
Table of Contents
Problem Statement
The Harshad or Niven numbers are positive integers ≥ 1 that are divisible by the sum of their digits. For example, 42 is a Harshad number as 42 is divisible by (4 + 2) without remainder. Assume that the series is defined as the numbers in increasing order.
The task is to create a function/method/procedure to generate successive members of the Harshad sequence. Use it to:
Show your output here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Harshad or Niven series step by step in the PL/M programming language
Source code in the pl/m programming language
100H:
/* FIND THE SUM OF THE DIGITS OF A 16-BIT NUMBER */
DIGIT$SUM: PROCEDURE(N) BYTE;
DECLARE N ADDRESS, SUM BYTE;
SUM = 0;
DO WHILE N > 0;
SUM = SUM + (N MOD 10);
N = N / 10;
END;
RETURN SUM;
END DIGIT$SUM;
/* FIND THE NEXT HARSHAD NUMBER ABOVE N */
NEXT$HARSHAD: PROCEDURE(N) ADDRESS;
DECLARE N ADDRESS;
NEXT:
N = N + 1;
IF N MOD DIGIT$SUM(N) = 0 THEN
RETURN N;
ELSE
GO TO NEXT;
END NEXT$HARSHAD;
/* CP/M SYSCALL */
BDOS: PROCEDURE(FUNC, ARG);
DECLARE FUNC BYTE, ARG ADDRESS;
GO TO 5;
END BDOS;
/* PRINT A STRING */
PRINT$STRING: PROCEDURE(STRING);
DECLARE STRING ADDRESS;
CALL BDOS(9, STRING);
END PRINT$STRING;
/* PRINT A NUMBER */
PRINT$NUMBER: PROCEDURE(N);
DECLARE S (7) BYTE INITIAL ('..... $');
DECLARE (N, P) ADDRESS, (C BASED P) BYTE;
P = .S(5);
DIGIT:
P = P - 1;
C = (N MOD 10) + '0';
N = N / 10;
IF N > 0 THEN GO TO DIGIT;
CALL PRINT$STRING(P);
END PRINT$NUMBER;
DECLARE CRLF DATA (13,10,'$');
DECLARE N ADDRESS INITIAL (0), S BYTE;
/* PRINT FIRST 20 HARSHADS */
CALL PRINT$STRING(.'FIRST 20: $');
DO S = 1 TO 20;
CALL PRINT$NUMBER(N := NEXT$HARSHAD(N));
END;
CALL PRINT$STRING(.CRLF);
/* PRINT HARSHAD NUMBER ABOVE 1000 */
CALL PRINT$STRING(.'FIRST ABOVE 1000: $');
CALL PRINT$NUMBER(NEXT$HARSHAD(1000));
CALL PRINT$STRING(.CRLF);
CALL BDOS(0,0);
EOF
You may also check:How to resolve the algorithm Repeat step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Textonyms step by step in the jq programming language
You may also check:How to resolve the algorithm Sleeping Beauty problem step by step in the Excel programming language
You may also check:How to resolve the algorithm Arrays step by step in the Dyalect programming language
You may also check:How to resolve the algorithm Proper divisors step by step in the Raku programming language