08-21-2007 11:12 AM
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.
08-21-2007 11: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
08-21-2007 11:26 AM
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.
08-21-2007 11:37 AM
vindo chk this logic one
len = strlen (str) .
do len times.
var = str + count(1).
count = count + 1.
sum = sum + var.
enddo.
08-21-2007 11: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
08-21-2007 11: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.
08-21-2007 11: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.
08-21-2007 11: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
08-21-2007 12:18 PM
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.............