So I am simplifying fractions for my fractions class and I am having trouble with the euclidian code for the gcd, it also doesn't print to the console window it just runs and nothing happens:

public class GCD {

    public static void main(String[]args)
    {
        int n = 5 ;
        int d = 6 ; 
        int larger, small ;
        if(n>d)
        {
            larger = n ;
            small = d ; 

        }
        else
        {   larger = d ; 
            small = n ; 
        }    
        while(small !=0)
        {
            int remainder = larger % small ;
            larger = small ; 
            small = larger ; 
        }
        System.out.print("The GCD of " + n + "and" +  d + "is larger") ; 
    }
}

Not sure what this loop:

    while(small !=0)
    {
        int remainder = larger % small ;
        larger = small ; 
        small = larger ; 
    }

is supposed to do, but larger and small never change, therefore small will never get to 0. All you're doing is swapping the values over and over again.

commented: Keen eye +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.