gcd of two numbers in java

Solutions on MaxInterview for gcd of two numbers in java by the best coders in the world

showing results for - "gcd of two numbers in java"
Elías
01 Apr 2020
1// gcd of two numbers in java
2import java.util.Scanner;
3public class GCDOfTwoNumbers 
4{
5   public static void main(String[] args) 
6   {
7      int a, b;
8      Scanner sc = new Scanner(System.in);
9      System.out.print("Please enter first number: ");
10      a = sc.nextInt();
11      System.out.print("Please enter second number: ");
12      b = sc.nextInt();
13      while(a != b) 
14      {
15         if(a > b)
16         {
17            a = a - b;
18         }
19         else
20         {
21            b = b - a;
22         }
23      }
24      System.out.println("GCD of two numbers in java: " + b);
25      sc.close();
26   }
27}
Alejandra
24 May 2016
1public class GCD {
2
3    public static void main(String[] args) {
4
5        int n1 = 81, n2 = 153, gcd = 1;
6
7        for(int i = 1; i <= n1 && i <= n2; ++i)
8        {
9            // Checks if i is factor of both integers
10            if(n1 % i==0 && n2 % i==0)
11                gcd = i;
12        }
13
14        System.out.printf("G.C.D of %d and %d is %d", n1, n2, gcd);
15    }
16}
queries leading to this page
find gcd using javajava math gcdfind gcd of three numbers in javagcd program in javagcd of two numbers java programcode to find gcd in javamath gcd in javafind gcd of two numbers in javahow to find gcd of 2 number in javajava program to find gcd of two numbersgcd calculator javahow to find gcd in javagcd of two numbers meansjava gcd of two numbersgcd method in java mathsgcd of three numbers in javagcd in java method in integer classprogram of gcd in javagcd of an array javagcd of n numbers in javajava program to find gcd of two numbergcd of two numbers program in javacondition for gcd in javagcd command javahow to find gcd javagreatest common divisor javagcd via iteratio java examplejava gcd built ingcd java codefind gcd in javahow to find gcd of two numbers in java directlyjava gcd functionwap to find gcd of two numbers in javahow to make gcd function in javagcd in java methodhow to write a while loop in java for gcdgcd array javagcd of two numbers in javaeasy way to find gcd in javahow to find the greatest common factor on javagcd in java mathcode for gcd in javagcd of array javagcd of two numbers in java efficientgcd in java inbuiltgcd of array java programhow to calculate gcd in javagcd java clasgcd function in java integergcd i javagcd of 2 numbers in javajava methods of calculating gcdgcd of a number in javafind gcd javafind gcd of two numbers in javagcd a number in javagcd of two numbersin javafinding gcd in javagcd in build in javahow to use math gcd in javacalculate gcd of two numbers javawhat is gcd in javajava gcd internal functionhcm in javagcd of two integers in javajava gcd programjava function to calculate gcdgcd syntax java gcd in javahow to find gcd of two numbers in javagcd java programjava program to calculate gcd of two numbersgcd java gfghow to find gcd of 2 numbers in javahow to find gcd of two elements in javagcd algorithm javafind gcd of two numbers javabuilt in gcd function in javagcd recursive javacalculate gcd javahow to write java program for gcd for positive and negative numbersjava code to find gcd of two nosgcd of 2 numbers in java using classesgcd and hcf javajava gcdjava program to find the gcd of numbersgcd of two numbers in java using while loopgcd of two numberswrite java code to calculate the hcf or the greatest common divisor of two numbers once you 27ve calculated the hcf of two numbers 2c print out the hcf gc polt java gcd fuction in javagcd question javagcd stl in javajava program to find gcd or hcf of 2 numberscalculate gcd in javagcd function in javagcd in java implementedhow to calculate gcd javaprogram to calulate gcd in javagcd of two numbers in java gfgjava method that give gcdjava code for gcdgcd method javagcd of two numbers javagcd of 2 numbers javafind the gcd of two numbers in javagcd function javagcd code in javahow to calculate gcd of two numbers in javagcd find in javafastest way to do gcd javawhat is gcd of two numbersgcd fn javahow to write a gcd program in javagcd javacd of two numbersfastest method to find gcd of two numbersgcd of two numbers using euclidean algorithm in javahow to find gcd of two numbers using constructor in javagcd and lcm of two numbers in javagcd in javagcd code javagcd of two numbers using constructor in javagcd of two numbers in java