How to resolve the algorithm Disarium numbers step by step in the COBOL programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Disarium numbers step by step in the COBOL programming language
Table of Contents
Problem Statement
A Disarium number is an integer where the sum of each digit raised to the power of its position in the number, is equal to the number.
135 is a Disarium number: 11 + 32 + 53 == 1 + 9 + 125 == 135 There are a finite number of Disarium numbers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Disarium numbers step by step in the COBOL programming language
Source code in the cobol programming language
IDENTIFICATION DIVISION.
PROGRAM-ID. DISARIUM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIABLES.
03 CANDIDATE PIC 9(9).
03 DIGITS PIC 9 OCCURS 9 TIMES, REDEFINES CANDIDATE.
03 IDX PIC 99.
03 EXPONENT PIC 99.
03 DGT-POWER PIC 9(9).
03 DGT-POWER-SUM PIC 9(9).
03 CAND-OUT PIC Z(8)9.
03 AMOUNT PIC 99 VALUE 18.
PROCEDURE DIVISION.
BEGIN.
PERFORM DISARIUM-TEST VARYING CANDIDATE FROM ZERO BY 1
UNTIL AMOUNT IS ZERO.
STOP RUN.
DISARIUM-TEST.
MOVE ZERO TO DGT-POWER-SUM.
MOVE 1 TO EXPONENT, IDX.
INSPECT CANDIDATE TALLYING IDX FOR LEADING ZEROES.
PERFORM ADD-DIGIT-POWER UNTIL IDX IS GREATER THAN 9.
IF DGT-POWER-SUM IS EQUAL TO CANDIDATE,
MOVE CANDIDATE TO CAND-OUT,
DISPLAY CAND-OUT,
SUBTRACT 1 FROM AMOUNT.
ADD-DIGIT-POWER.
COMPUTE DGT-POWER = DIGITS(IDX) ** EXPONENT.
ADD DGT-POWER TO DGT-POWER-SUM.
ADD 1 TO EXPONENT.
ADD 1 TO IDX.
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the TypeScript programming language
You may also check:How to resolve the algorithm Collections step by step in the Maple programming language
You may also check:How to resolve the algorithm Tokenize a string with escaping step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Algae programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the PowerShell programming language