iterative program on java

65 Views Asked by At

i have this program

public static int p(int n, int m){
    if(n==m) return n;
    if (n<m) return p(n,m-n);
    else return p(n-m,m);


}

how to put this program on iterative program with while loop. Thanks

1

There are 1 best solutions below

0
On

This code substacts the smaller of the two inputs from the larger until they are equal. This can be done with a while loop:

public static int p(int n, int m){
    while (m!=n) {
        if (n<m)
            m -= n;
        else
            n -= m;
    }
    return n;
}