Can someone please provide the code for this in Java Thank y

Can someone please provide the code for this in Java?? Thank you.

Finding prime factors: prime numbers are positive integers whose integral factors are only 1 and itself (excluding 1; the first prime number is 2, and it is the only even prime number). The prime factorization theorem states that every positive integer is uniquely factorable into a product of prime numbers. In this exercise, you are asked to find ALL the prime integral factors of a positive integer (hence 0 is not included as a possible input, but in addition 1 and the number itself are to be included as outputs). For the user input of 40, the output should be: 1, 2, 2, 2, 5, and 40. Store these integral factors of a number the user supplies in a sorted List: List<Integer> flist = new ArrayList<Integer>(); then display the contents of this list. Next find and display the list of all factors of the integer input; for 40 that would be: 1, 2, 4, 5, 8, 10, 20, and 40.

Solution

Hi, Please find my code.

Please let me know in case of any issue.

import java.util.ArrayList;

public class PrimeFactorization {

  

   public static void main(String[] args) {

      

       ArrayList<Integer> factors = new ArrayList<>();

      

       // adding 1 in list

       factors.add(1);

      

       int s = 2;

      

       int num = 40;

       int n = num;

      

       // loop till divisors is less than dividend

       while(s <= n){

          

           // divide n by s till it is divisible

           while(n !=1 && n%s==0){

               factors.add(s);

               n = n/s;

           }

          

           s = s+1;

       }

      

       factors.add(num); // adding number itself in list

      

       System.out.println(factors);

      

      

       // finding factors

       for(int i=1; i<=num; i++){

           if(num%i == 0)

               System.out.print(i+\" \");

       }

       System.out.println();

      

   }

}

/*

Sample output:

[1, 2, 2, 2, 5, 40]

1 2 4 5 8 10 20 40

*/

Can someone please provide the code for this in Java?? Thank you. Finding prime factors: prime numbers are positive integers whose integral factors are only 1 a
Can someone please provide the code for this in Java?? Thank you. Finding prime factors: prime numbers are positive integers whose integral factors are only 1 a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site