Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: GCD of Two Numbers in Java

  1. #1
    Junior Member
    Join Date
    Dec 2020
    Location
    Gwalior
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default GCD of Two Numbers in Java

    Hello All, I am stack in gcd of two numbers problem. The problem statement is two non-negative integers a and b, we have to find their GCD (greatest common divisor),i.e. the largest number which is a divisor of both a and b. It’s commonly denoted by gcd(a,b) Check the code and Please suggest me, Is it right or not? I am using to solve this problem by Euclid’s Algorithm.

    Taken this code reference from here - https://www.interviewbit.com/blog/gcd-of-two-numbers/


    import java.util.*;
    import java.lang.*;

    class Interviewbit {
    // extended Euclidean Algorithm
    public static int gcd(int a, int b) {
    if (a == 0)
    return b;

    return gcd(b % a, a);
    }

    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: GCD of Two Numbers in Java

    Do you have any specific java programming questions?
    Do you have an algorithm that you are trying to write using java?


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2022
    Posts
    41
    Thanks
    1
    Thanked 3 Times in 2 Posts

    Default Re: GCD of Two Numbers in Java

    int gcdByEuclidsAlgorithm(int n1, int n2) {
        if (n2 == 0) {
            return n1;
        }
        return gcdByEuclidsAlgorithm(n2, n1 % n2);
    }

    Should be functionally identical.
    Last edited by AngleWyrm; July 4th, 2022 at 11:36 AM.

Similar Threads

  1. Replies: 3
    Last Post: November 18th, 2013, 11:55 AM
  2. Replies: 4
    Last Post: February 10th, 2013, 11:58 PM
  3. [SOLVED] Write a java program to display even numbers between 2 and 200 using netbeans java
    By Shalom in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 11th, 2012, 05:16 AM
  4. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM