Skip to Content
avatar image
Former Member

Help me to add numbers

hi all,

can anybody tell me the logic to impliment following thing in zreport.

eg= number = 12345.

addition = 1 + 2 + 3 + 4 + 5.

result = 15.

please give me the logic to add digits of the number.

thanks in advance.

regds.

Vinod.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • avatar image
    Former Member
    Aug 21, 2007 at 10:17 AM

    Hi Vinod,

    Move the numaric value to character field, and get the length of the number...

    do n times.

    result = result + number+n(1).

    enddo.

    Regards,

    Satya

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      vindo chk this logic one

      len = strlen (str) .

      do len times.

      var = str + count(1).

      count = count + 1.

      sum = sum + var.

      enddo.

  • avatar image
    Former Member
    Aug 21, 2007 at 10:18 AM

    HI vinod

    first find the length

    len = strlen (str) .

    do len times.

    var = str + count(1).

    count = count + 1.

    sum = sum + var.

    enddo.

    reward points to all helpful answers

    kiran.M

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 21, 2007 at 10:26 AM

    hi,

    try like this

    data: numb type i value '12345',

    sum type i.

    while numb > 0.

    numb = numb %10. [ modulo division --- remainder odf division]

    sum = sum + numb.

    numb = numb /1 0.

    endwhile

    iwrite:/10 sum.

    if helpful reward some points.

    with regards,

    Suresh Aluri.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 21, 2007 at 10:28 AM

    Hi Vinod,

    The below code will satisfy your requirement.

    give any number as parameter, it will give the sum of all digits.

    REPORT zkk_sum.

    parameters: p_num type i.

    data: v_num(30) type c,

    v_length type i,

    v_sum type i,

    v_count type i.

    move p_num to v_num.

    v_length = strlen( v_num ).

    do v_length times.

    v_count = v_count + 1.

    v_sum = v_sum + ( v_num+v_count(1) ).

    enddo.

    write:/ 'The Sum of ', p_num, 'is ', v_sum.

    Add comment
    10|10000 characters needed characters exceeded

  • Aug 21, 2007 at 10:31 AM

    Hi,

    Try the below code its working.

    DATA: char(5) TYPE c VALUE '12345'.
    
    DATA: len TYPE i.
    
    DESCRIBE FIELD char LENGTH len in CHARACTER MODE.
    DATA: count type i.
    do len times.
    data: n type n.
    data: i type i.
    data: in type i.
    in = sy-index - 1.
    n = char+in(1).
    i = n.
    count = count + i.
    enddo.
    
    write: count.
    

    Regards,

    Sesh

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 21, 2007 at 11:18 AM

    hi vinod

    data : c type str value '12345',

    result type i.

    data : count type i value '0'.

    do len times.

    var = str + count(1).

    count = count + 1.

    result = result+ var.

    enddo.

    write : result.

    reward if useful.............

    Add comment
    10|10000 characters needed characters exceeded