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.

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

GCD

Former Member

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2007 Mar 14 12:36 PM

1 ACCEPTED SOLUTION

Former Member

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2007 Mar 14 12:51 PM

150

- SAP Managed Tags:
- ABAP Development

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2007 Mar 14 12:44 PM

Former Member

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2007 Mar 14 12:48 PM

Former Member

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2007 Mar 14 12:51 PM

151

- SAP Managed Tags:
- ABAP Development

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2007 Mar 14 1:39 PM

150

- SAP Managed Tags:
- ABAP Development

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2007 Mar 15 4:28 AM

150

- SAP Managed Tags:
- ABAP Development

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2007 Mar 15 8:27 AM

Former Member

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2007 Mar 15 6:53 AM

150

- SAP Managed Tags:
- ABAP Development

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.

:):)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

2007 Mar 15 8:20 AM