Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java method called glaciered that takes two integer arguments k and m ,

ID: 3912048 • Letter: W

Question

Write a Java method called glaciered that takes two integer arguments k and m, both of which you may
assume are positive values. Return the largest integer that is both a factor of k and a factor of m.

Sample Data #1:
If k = 12 and m = 30, then all of the following are both factors of k and m: 1, 2, 3, and 6. The
return value here would be 6, since that is the largest factor of both m and k.

Sample Data #2:
If k = 80 and m = 20. The common factors are: 1, 2, 4, 5, 10, and 20, so the method would
return 20.

Sample Data #3:
If: k = 15 and m = 32. The only common factor is 1, so the method would return 1 here.

public static void main(String [] args)
   {
      int k = 0;
      int m = 0;
      System.out.println("Sample Data #1 returns: " + glaciered(12, 30));
      System.out.println("Sample Data #2 returns: " + glaciered(80, 20));
      System.out.println("Sample Data #3 returns: " + glaciered(15, 32));
   }
   public static int glaciered (int k, int m)
   {

Explanation / Answer

class Main {

public static int glaciered (int k, int m)

{

// FOR setting k to have smaller value

if(k > m )

{

int temp = k;

k = m;

m = temp;

}

// going from smaller number to 1

for(int i=k; i>=1; i--)

{

// when there is a factor, return right away

if(m%i == 0 && k%i == 0)

{

return i;

}

}

return 1;

}

public static void main(String [] args)

{

int k = 0;

int m = 0;

System.out.println("Sample Data #1 returns: " + glaciered(12, 30));

System.out.println("Sample Data #2 returns: " + glaciered(80, 20));

System.out.println("Sample Data #3 returns: " + glaciered(15, 32));

}

}

/*SAMPLE OUTPUT

Sample Data #1 returns: 6

Sample Data #2 returns: 20

Sample Data #3 returns: 1

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote