Examples of factorization and least common multiple using Java code

  • 2020-04-01 04:23:37
  • OfStack

factorization


 
public class  factorization  { 
 public static void f(int n) { 
  for (int i = 2; i < n / 2; i++) { 
   while(n%i==0){ //Fill in the blanks
    System.out.printf("%d ", i); 
    n = n / i; 
   } 
  } 
  if (n > 1) 
   System.out.printf("%dn", n); 
 } 
 public static void main(String[] args) { 
  f(60); 
 } 
} 

Operation results:


2 2 3 5 

Least common multiple


 
public class  Least common multiple  { 
 public static int f(int a, int b) 
 { 
  int i; 
  for(i=a;;i+=a){ //Fill in the blanks
   if(i%b==0) return i; 
  } 
 } 
 public static void main(String[] args){ 
  System.out.println(f(6,8)); 
 } 
} 

Operation results:

24


Related articles: