A number is a factor of another number if it evenly divides

A number is a factor of another number if it evenly divides that number. For example, 3 is a factor of 12
because 12 % 3 is 0. Finish the method below to return the smallest factor of n that is larger than k.
(Hint: do not print anything. Find the smallest factor using a loop, and then return it. You may assume
that k < n.)

public static int smallestFactor(int n, int k)

Solution

package sample;
public class lib {   

public static void main(String args[]) {
smallestFactor(12,3);
}
public static int smallestFactor(int n, int k){
int fact=n;
   for(int i=k+1;k<=n;k++){
   if(n%i==0)
   {
       fact=i;
   }  
}
return fact;
}
}

A number is a factor of another number if it evenly divides that number. For example, 3 is a factor of 12 because 12 % 3 is 0. Finish the method below to return

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site