Create fucntion to find GCD of two numbers

0 votes
Can someone help me write the function to find the GCD of two numbers?
Mar 28, 2019 in Java by Diya
888 views

1 answer to this question.

0 votes

Hey @Diya, here is a code on how you can achieve this,

public class GCD { //class name is GCD
    static int gcd (int a, int b) //creating a function 
    {
        if (b==0) // if b is 0 the gcd will be a
            return a;
        else
            return gcd(b,a%b); // if b is not 0 then find the modulus and keep doing it till a%b becomes 0
    }
    public static void main(String[] args) //main method
    {
        int a =10 , b=4; //two variables
        System.out.println(gcd (a,b)); //calling the function
    }

}

Hope this helps.

answered Mar 28, 2019 by Priyaj
• 58,020 points

Related Questions In Java

0 votes
1 answer

In Java, how to find out the min/max values from array of primitive data types?

import java.util.Arrays; import java.util.Collections; import org.apache.commons.lang.ArrayUtils; public class MinMaxValue { ...READ MORE

answered Jun 12, 2018 in Java by Daisy
• 8,140 points
1,656 views
0 votes
0 answers

How to manage two JRadioButtons in java so that only one of them can be selected at a time?

How to manage two JRadioButtons in java ...READ MORE

Apr 21, 2020 in Java by kartik
• 37,520 points
713 views
0 votes
0 answers

how to find lenght of string in java ?

java READ MORE

Nov 12, 2021 in Java by anonymous
• 210 points
535 views
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,620 points
1,202 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,337 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,198 views
+1 vote
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,680 points
3,760 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,609 views
+4 votes
4 answers

Using while loop i want to print 10 even numbers

Hello @Nitesh, you are pretty much there. ...READ MORE

answered Nov 22, 2018 in Java by Priyaj
• 58,020 points
93,158 views
+4 votes
1 answer

How to print the individual occurance of elements in Java?

You can use a HashMap to serve ...READ MORE

answered Dec 4, 2018 in Java by Priyaj
• 58,020 points
1,247 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP