Write a program to find GCD (or HCF) of Two Numbers In Java


In this core java programming tutorial we will Write a program to find GCD (or HCF) of Two Numbers In Java.


Contents of page >
  • Full Program/SourceCode / Example 1 to find GCD (or HCF) of Two Numbers In Java
  • Full Program/SourceCode / Example 2 to find GCD (or HCF) of Two Numbers In Java
  • Full Program/SourceCode / Example 3 to find GCD (or HCF) of Two Numbers RECURSIVELY In Java


Example 1>
Enter first number :
21
Enter second number :
14
GCD of 21 and 14 is =
7


Example 2>
Enter first number :
42
Enter second number :
28
GCD of 42 and 28 is =
14


Example 3>
Enter first number :
65
Enter second number :
39
GCD of 65 and 39 is =
13





Full Program/SourceCode / Example 1 to find GCD (or HCF) of Two Numbers In Java >
import java.util.Scanner;
/*
* Write a program to find GCD (or HCF) of Two Numbers In Java
* GCD means Greatest common divisor
* HCF means highest common factor
*/
public class GCDOfTwoNumbersInJavaExample {
   public static void main(String args[]) {
          Scanner scanner = new Scanner(System.in);
          System.out.println("Enter first number : ");
          int firstNumber = scanner.nextInt();
          System.out.println("Enter second number : ");
          int secondNumber = scanner.nextInt();
          System.out.println("GCD of " + firstNumber + " and " + secondNumber
                       + " is = ");
         
          /*
          * Now we will calculate GCD (or HCF) of Two Numbers In Java
          */
          int remainder;
          //Use while loop to find GCD (or HCF)
          while (secondNumber != 0) {
                 remainder = firstNumber % secondNumber;
                 firstNumber = secondNumber;
                 secondNumber = remainder;
          }
          System.out.println(firstNumber);
   }
}
/*OUTPUT
Enter first number :
18
Enter second number :
12
GCD of 18 and 12 is = 6
*/


Full Program/SourceCode / Example 2 to find GCD (or HCF) of Two Numbers In Java >
import java.util.Scanner;
/*
* Write a program to find GCD (or HCF) of Two Numbers In Java
*/
public class GCDOfTwoNumbersInJavaExample2 {
   public static void main(String args[]) {
          Scanner scanner = new Scanner(System.in);
          System.out.println("Enter first number : ");
          int firstNumber = scanner.nextInt();
          System.out.println("Enter second number : ");
          int secondNumber = scanner.nextInt();
          System.out.println("GCD of " + firstNumber + " and " + secondNumber
                       + " is = ");
          /*
          * Now we will calculate GCD (or HCF) of Two Numbers In Java
          */
          //USe while loop to find GCD (or HCF)
          while (firstNumber != secondNumber) {
                 if (firstNumber > secondNumber)
                       firstNumber = firstNumber - secondNumber;
                 else
                       secondNumber = secondNumber - firstNumber;
          }
          System.out.println(firstNumber);
   }
}
/*OUTPUT
Enter first number :
18
Enter second number :
12
GCD of 18 and 12 is = 6
*/


Full Program/SourceCode / Example 3 to find GCD (or HCF) of Two Numbers RECURSIVELY In Java
import java.util.Scanner;
/*
* Write a program to find GCD (or HCF) of Two Numbers recursively In Java
*/
public class GCDOfTwoNumbersRecursivelyInJava {
   public static void main(String args[]) {
          Scanner scanner = new Scanner(System.in);
          System.out.println("Enter first number : ");
          int firstNumber = scanner.nextInt();
          System.out.println("Enter second number : ");
          int secondNumber = scanner.nextInt();
          System.out.println("GCD of " + firstNumber + " and " + secondNumber
                       + " is = " + findGCD(firstNumber, secondNumber));
   }
   /**
   * Now this method will calculate GCD (or HCF) of Two Numbers In Java
   */
   static int findGCD(int firstNumber, int secondNumber) {
          // If in case second number becomes 0 then first number is gdf
          if (secondNumber == 0) {
                 return firstNumber;
          }
          int remainder = firstNumber % secondNumber;
          return findGCD(secondNumber, remainder);
   }
}
/*OUTPUT
Enter first number :
18
Enter second number :
12
GCD of 18 and 12 is = 6
*/



So, in this core java programming tutorial we wrote a program to find GCD (or HCF) of Two Numbers In Java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

>Pattern/Pyramid generating programs



No comments:

Post a Comment