Answers for "how to find prime factors in java"

5

prime factorization java

import java.util.Scanner;

public class PrimeFactors {
   public static void main(String args[]){
      int number;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a number ::");
      number = sc.nextInt();
      
      for(int i = 2; i< number; i++) {
         while(number%i == 0) {
            System.out.println(i+" ");
            number = number/i;
         }
      }
      if(number >2) {
         System.out.println(number);
      }
   }
}
Posted by: Guest on June-06-2020
4

calculate prime factors of a number java

public static void main(String[] args) {
   	int n=12;
    for(int i=2; i<=n; i++){ 
       while(n%i==0){
         n=n/i;
         System.out.println(i); //prints 2 2 3
       }
   }
}
Posted by: Guest on June-17-2021

Code answers related to "how to find prime factors in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language