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

using mod in the calculation

Former Member
0 Likes
2,899

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.

9 REPLIES 9
Read only

Former Member
0 Likes
2,378

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.

Read only

0 Likes
2,378

var1 = varlen(1) + varlen(1) + varlen(5) + varlen(7). this is giving wrong result.

please anyone tell me another logic

Read only

0 Likes
2,378

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.

Read only

0 Likes
2,378

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

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,378

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

Read only

Former Member
0 Likes
2,378

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

Read only

0 Likes
2,378

Hi Rob,

It might be a check algorithm logic, i have gone through such requirement for a bank key validation.

Read only

0 Likes
2,378

Good point.

Rob

Read only

former_member194797
Active Contributor
0 Likes
2,378

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.