‎2010 Jan 19 10:20 AM
experts
810312301234X
I need to calculate the x value in the above field .
logic is as shown below.
First we need to add all the odd numbers starting from left ie in our above example it is 4 because x we should not consider so first number is 4.
var1 = sum of odd(42023+1) = 10
First we need to add all the even numbers starting from left here in our case 3 is the first even number and x we should not consider.
var2 = sum of even(31310+8) = 16
var3 = var13 + var2 = 103 + 16 = 46
finally we need to take the mod value ie the remainder and that is our desired value X.
var4 = mod(var3/10) = mod(46/10) = 6
x= var4 = 6
Now please tell me how to do this easily in sap ie how to calculate this.
thank you for the replies.
‎2010 Jan 19 10:59 AM
var = 810312301234X
Get the length of the variable using strlen
varlen = strlen(var)
Add all odd digits using the positioning syntax
Eg -
var1 = varvarlen(1)....
same case for var2 also add all the even digits.
var3 = (var1 x 3) + var2
var4 = mod(var3/10)
vartemp = varlen - 1.
replace x by var4 - concatenate var++(vartemp) var4 into var.
Hope it helps you.
‎2010 Jan 19 12:13 PM
var1 = varlen(1) + varlen(1) + varlen(5) + varlen(7). this is giving wrong result.
please anyone tell me another logic
‎2010 Jan 19 12:25 PM
it should be var+varlen(1)
Eg - var+2(1) which is the third digit. after 2 place 1 digit.
var+3(5) which is startinf from third position, 5 places.
‎2010 Jan 19 1:03 PM
Hi Shiva,
Check this logic.
DATA: varx TYPE c LENGTH 10 VALUE '12345x',
var1 TYPE I,
var2 TYPE I,
var3 TYPE I,
cnt TYPE I,
mod1 TYPE I,
x TYPE I.
cnt = STRLEN( varx ) - 1.
DO cnt TIMES.
mod1 = var2 mod 2.
IF mod1 eq 0. " for even numbers
var1 = varx+var2(1) + var1.
ELSE. " for odd numbers
var3 = varx+var2(1) + var3.
ENDIF.
var2 = var2 + 1.
ENDDO.
x = ( ( var3 * 3 ) + var1 ) mod 10.
Regards,
Swarna Munukoti
‎2010 Jan 19 1:39 PM
Check this for logic
data:lv_input type string.
data:lv_index type i,
lv_odd_pos type string,
lv_even_pos type string,
lv_result type string,
lv_flag type char01.
lv_input = '123456789X'.
lv_index = strlen( lv_input ) - 2.
if lv_index > 0.
do.
if lv_flag is initial.
lv_odd_pos = lv_odd_pos + lv_input+lv_index(1).
condense lv_odd_pos.
lv_flag = 'X'.
else.
lv_even_pos = lv_even_pos + lv_input+lv_index(1).
condense lv_even_pos.
clear lv_flag.
endif.
if lv_index = 0.
exit.
endif.
lv_index = lv_index - 1.
enddo.
else.
lv_odd_pos = lv_input+lv_index(1).
endif.
lv_result = ( ( lv_odd_pos * 3 ) + lv_even_pos ) ) MOD 10.
Edited by: Keshav.T on Jan 19, 2010 8:05 PM
‎2010 Jan 19 2:50 PM
This appears to be a homework question rather than business related. If it is indeed business related, please let us know the business case.
Rob
‎2010 Jan 19 2:55 PM
Hi Rob,
It might be a check algorithm logic, i have gone through such requirement for a bank key validation.
‎2010 Jan 19 3:22 PM
‎2010 Jan 20 11:22 AM
DATA: STR1(20) VALUE '810312301234X'.
DATA: STR2(20).
DATA: VAR1 TYPE I, VAR2 TYPE I, VAR3 TYPE I, VAR4 TYPE I.
STR2 = STR1.
SHIFT STR2 RIGHT DELETING TRAILING SPACE.
SHIFT STR2 RIGHT BY 1 PLACES. " suppress trailing 'X'.
WHILE STR2 NE SPACE.
SHIFT STR2 RIGHT BY 2 PLACES CIRCULAR.
IF STR2+1(1) CO '0123456789'. "numeric ?
ADD STR2+1(1) TO VAR1.
ENDIF.
IF STR2(1) CO '0123456789'. "numeric ?
ADD STR2(1) TO VAR2.
ENDIF.
CLEAR STR2(2).
ENDWHILE.
VAR3 = 3 * VAR1 + VAR2.
VAR4 = VAR3 MOD 10.
WRITE: / VAR1, VAR2, VAR3, VAR4.
please note that in my system 42023+1 = 12 and not 10.