Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Java Greatest Common Factor Two Numbers

$
0
0
Hello, trying to figure out why this program isn't working. Most likely a logic error because I am terrible at gcds and primes. Please take a look?

public static int maxFactor(int a, int B)/>
        {
                System.out.println("Enter two numbers and I will find the largest prime factor.");
                Scanner kb = new Scanner(System.in);
                a = kb.nextInt();
                b = kb.nextInt();
                int g = gcd(a,B)/>;
                int divider = 2;
                int prime = 1;

                while(g >= divider)
                {
                        if (g%divider == 0)
                        {
                                prime = divider;
                                g /= divider;
                        }
                        else
                        {
                                divider++;
                        }
                }
                System.out.println(g);
                return g;
        }

 public static int gcd(int a, int B)/>
        {
                if (b == 0)
                {
                        return a;
                }
                return gcd(b, a%B)/>;
                
        }


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>