Application Development 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: 

Help me to add numbers

Former Member
0 Kudos

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.

8 REPLIES 8

Former Member
0 Kudos

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

0 Kudos

thanks for reply

error occurs at that statement number+n(1). saying that n is no a numaric litaral or constant.

what can i do.

0 Kudos

vindo chk this logic one

len = strlen (str) .

do len times.

var = str + count(1).

count = count + 1.

sum = sum + var.

enddo.

Former Member
0 Kudos

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

Former Member
0 Kudos

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.

Former Member
0 Kudos

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.

0 Kudos

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

Former Member
0 Kudos

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.............