Created
May 4, 2013 12:39
-
-
Save jimmy087/5517378 to your computer and use it in GitHub Desktop.
Greatest Common Divisor in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| // Calculating Greatest Common Divisor. | |
| public class GreatestCommomDivisor { | |
| public static void main(String[] args) { | |
| Scanner input = new Scanner(System.in); | |
| System.out.print("Please enter the first integer: "); | |
| int number1 = input.nextInt(); | |
| System.out.print("Please enter the second integer: "); | |
| int number2 = input.nextInt(); | |
| int gcd = 1; | |
| for (int k = 2; k <= number1 && k <= number2; k++) { | |
| if(number1 % k == 0 && number2 % k == 0) gcd = k; | |
| } | |
| System.out.print("The greatest common divisor for " + number1 + " and " + number2 + " is " + gcd); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment