19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

190 Chapter 5 Methods<br />

invoke gcd<br />

<strong>com</strong>pute gcd<br />

return gcd<br />

15 System.out.println("The greatest <strong>com</strong>mon divisor for " + n1 +<br />

16 " and " + n2 + " is " + gcd(n1, n2) );<br />

17 }<br />

18<br />

19 /** Return the gcd of two integers */<br />

20<br />

21<br />

public static int gcd(int n1, int n2) {<br />

int gcd = 1; // Initial gcd is 1<br />

22 int k = 2; // Possible gcd<br />

23<br />

24 while (k

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!