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: 

GCD

Former Member
0 Kudos

how to write program in abap to find GCD of three numbers

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

What's the reason for asking this question: are you going to teach for some school children?

Try your self to find the GREATEST COMMON DIVISIOR of three numbers.

Take 3 varaibles: let say a1, a2. a3.

Do a1 times.

You start dividing continuously all these three numbers .

when ever you get the last number which devides these three numbers, that is the GCD.

endddo.

reward if useful

Regards,

Anji

8 REPLIES 8

Former Member
0 Kudos

Looks as if I will have to ask.

What is GCD.

Former Member
0 Kudos

martin its

greatest common divisor

Former Member
0 Kudos

Hi,

What's the reason for asking this question: are you going to teach for some school children?

Try your self to find the GREATEST COMMON DIVISIOR of three numbers.

Take 3 varaibles: let say a1, a2. a3.

Do a1 times.

You start dividing continuously all these three numbers .

when ever you get the last number which devides these three numbers, that is the GCD.

endddo.

reward if useful

Regards,

Anji

0 Kudos

*A crude way of doing GCD. Can use paramters on selection screen if reqd

data: n1 type i, " First Number

n2 type i, " Second Number

n3 type i, " Third Number

n4 type i, " Find reminder for first number

n5 type i, " Find reminder for Second Number

n6 type i, " Find reminder for third number

n7 type i, " Stores GCD

n type i. " Counter

n = 2.

n1 = 30.

n2 = 15.

n3 = 45.

do.

n4 = n1 mod n." Divide by counter

n5 = n2 mod n.

n6 = n3 mod n.

*Stop if the counter is greater than any given number.

if sy-index gt n1 or sy-index gt n2 or sy-index gt n3.

exit.

endif.

if n4 = 0 and n5 = 0 and n6 = 0.

n7 = n.

endif.

n = n + 1.

enddo.

write n7.

0 Kudos

Hi ,

Am not going to teach school children. Am myself learning ABAP so i want to practice some logics in ABAP so i tried for that but i cant able to get so only i asked.

0 Kudos

hi, thanks

Former Member
0 Kudos

Hi ,

Execute the code .

REPORT zex13 .


PARAMETERS : a TYPE i ,
             b TYPE i ,
             c TYPE i .

DATA : d TYPE i,
       low TYPE i.
DATA : r1 TYPE i ,
       r2 TYPE i ,
       r3 TYPE i .



* find the least of the three numbers .

IF a < b.
  low = a.
ELSE .
  low = b.
ENDIF.

IF c < low .
  low = c.
ELSE .
  low = low.
ENDIF.


*find the gcd.

WHILE ( low >= 1 )   .

  r1 =  a MOD low.

  r2 =  b MOD low.

  r3 =  c MOD low.

  IF r1 = 0 AND r2 = 0 AND r3 = 0.
    WRITE 😕 'Gcd of', a, b , c , 'is' , low.
    EXIT.
   
  ENDIF.
  low = low - 1.

ENDWHILE.

Steps .

For ur query 


1. First find the least value of the given 3 numbers .

2. Divide the 3 numbers with this  low  value .

3.See the GCD cannot be greater than the low value at most it can be  equal to or less than the low value .    
       
4 . GCD of the 3 numbers is when u have a zero reminder for all the numbers .

      We will search this condition keeping in loop by approaching the Low value in the reverse till we get the condition true i.e R1 = 0 ,R2 = 0, R3 = 0.

5. Once a hit then exit out of the loop .
    that is ur gcd .

6. When this condition also is failing then GCD can be only 1 for the exception .


I hope this will solve ur query .

Still you can enhance the logic to the above code.

Revert back for further queries .

Regards,

Vijay.

:):)

0 Kudos

Hi vijay,

Thanks its working fine.