Skip to Content
-1
Jan 31, 2021 at 10:17 AM

How to count digits in a number and get the digit counted as the power of n

165 Views

Hi, I would like to ask, how i want to count digit in a number and make it a power of n.

For example:

1. user input 153

2. Total number of digit is 3

3. Sum = 1^3 + 5^3 + 3^3 (the number 3 just now, will be the power of n)

PARAMETERS: lv_data1(10) TYPE p.

DATA:

lv_digit(10) TYPE i,

lv_final(1) TYPE i,

lv_arm(10) TYPE i,

lv_len TYPE i,

lv_data2(10) TYPE p.

START-OF-SELECTION

lv_data2 = lv_data1.

lv_len = STRLEN(lv_data2).

WHILE lv_data2 <> 0.

lv_digit = lv_data2 MOD 10.

lv_arm = lv_arm + ( lv_digit**lv_len).

lv_data2 = trunc( lv_data2 / 10 ).

ENDWHILE.

IF lv_data1 EQ lv_arm.

WRITE: 'It is an Armstrong Number'.

ELSE.

WRITE: 'It is not an Armstrong Number'.

ENDIF.

END-OF-SELECTION.