Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program that calculates the greatest common divisor (GCD)
- * of given two integers a and b. Use the Euclidean algorithm. */
- import java.util.Scanner;
- public class _17_CalculateGreatestCommonDivisorOfGiven2Integers {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter a 2 Integer numbers: ");
- int numA = scan.nextInt();
- int numB = scan.nextInt();
- scan.close();
- numA = Math.abs(numA);
- numB = Math.abs(numB);
- if (numA != numB) {
- if (numB > numA) {
- numA += numB;
- numB = numA - numB;
- numA -= numB;
- }
- // Using Euclidean algorithm:
- while (numB > 0) {
- int remaider = numA % numB;
- numA = numB;
- numB = remaider;
- }
- }
- System.out.println("The Greatest common Divisor of these 2 numbers is: "
- + numA);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement