Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Standard function or logic required

Former Member
0 Likes
543

Hi all,

I have one number (9876), now i need to get the sum of this number.That is 98765 that is 35.so i need to extract 3+5 = 8. si finalli i need get 8

can anybody give me some logic (sample code) or some predefiend functions for this.

Many Thanks,

Ragh

4 REPLIES 4
Read only

JozsefSzikszai
Active Contributor
0 Likes
517

hi Raghu,

there could be simplier and nicer solution, but it works:

PARAMETERS : i TYPE i.

DATA : s(20) TYPE c,

result TYPE i,

length TYPE i.

START-OF-SELECTION.

s = i.

SHIFT s LEFT DELETING LEADING space.

result = 10.

WHILE result GT 9.

result = 0.

length = STRLEN( s ).

DO length TIMES.

result = result + s(1).

SHIFT s CIRCULAR BY 1 PLACES.

ENDDO.

s = result.

SHIFT s LEFT DELETING LEADING space.

ENDWHILE.

Now you have number you want in result (integer format) and s (character format)

hope this helps

ec

Read only

Former Member
0 Likes
517

Hi,

Take a look at function module 'BKK_COMPUTE_SUM_OF_DIGITS'. I'm not sure you should use the function module directly (CAUTION: It is not released by SAP!), but you can use the same logic or copy its source code for use in your own method/function module.


*---- calculate sum of digits for given number with maximum 4 digits --
  field-symbols <f>.

  data: l_ref(4) type n,
        l_sum    type i value 0.

  l_ref = i_number.
  assign l_ref+0(1) to <f>.
  l_sum = l_sum + <f>.

  do 3 times.
    assign <f>+1 to <f>.
    l_sum = l_sum + <f>.
  enddo.

Here's another way for you to consider. It is different than the code within the SAP function module but same concept (without field-symbols):


PARAMETERS: p_num(4) TYPE n.

DATA: l_sum  TYPE i,
      l_next TYPE i.

l_sum = p_num(1).

DO 3 TIMES.
  l_next = sy-index.
  l_sum = l_sum + p_num+l_next(1).
ENDDO.

WRITE: /1 'The digits of', p_num, 'add up to:', l_sum.

Regards,

Jamie

Edited by: James Gaddis on Feb 29, 2008 11:19 AM -- Added the code for quick reference and in case you are using pre-ECC 5.0 system.

Edited by: James Gaddis on Feb 29, 2008 2:07 PM -- Added an alternative coding method.

Read only

Former Member
0 Likes
517

Hi Ragh,

Interesting question. Just put this together for you. It works nicely. Plus, it will work for numbers any length (you just need to change the (20) if your starting number will be longer than 20 digits.

Hope it helps.

SL


REPORT  Z_COUNT_TEST.

DATA : NUMBER(20) TYPE C VALUE '98765',
       SUM TYPE I,
       LEN TYPE I,
       POINTER TYPE I VALUE '0'.

WHILE STRLEN( NUMBER ) NE '1'.
CLEAR : LEN, SUM, POINTER.
LEN = STRLEN( NUMBER ).
DO LEN TIMES.
  SUM = SUM + ( NUMBER+POINTER(1) ) .
  POINTER = POINTER + 1.
ENDDO.

NUMBER = SUM.
CONDENSE NUMBER NO-GAPS.

ENDWHILE.

WRITE: 'SUM:' , SUM.

Read only

Former Member
0 Likes
517

Another one -:)


DATA : number(20) TYPE c VALUE '98765',
       sum TYPE i,
       len TYPE i,
       w_tabix TYPE sy-tabix.

len = strlen( number ).

WHILE w_tabix LT len.
  DO len TIMES.
    sum = sum + number+w_tabix(1).
    w_tabix = w_tabix + 1.
  ENDDO.
  IF w_tabix EQ len.
    number = sum.
    CONDENSE number.
    len = strlen( number ).
    IF len EQ 1.
      EXIT.
    ENDIF.
    CLEAR: w_tabix,sum.
  ENDIF.
ENDWHILE.

WRITE:/ sum.

Greetings,

Blag.