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

I posted this earlier but I appologize that I did not specify that this should b

ID: 3683741 • Letter: I

Question

I posted this earlier but I appologize that I did not specify that this should be Java.

Create a program which prompts the user for a number. The program then calculates the prime factors of that number USING RECURSION. If you don't use recursion in your program, you will not get credit. Your program should then output the prime factors in sorted order. For example, the prime factors for 6 are 2x3. For 12, 2 is a prime factor, leaving 6 (2x6=12). So, you could then determine the prime factors of 6, resulting in 2 and 3 as the prime factors. Thus, the factors are 2, 2, 3. This example also gives you a hint regarding a recursive algorithm.

Explanation / Answer

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
   public static void main (String[] args) throws java.lang.Exception
   {
       Scanner scan = new Scanner(System.in);
       int n = scan.nextInt();
       primefact(n);
   }
   public static void primefact(int n)
   {
       if(n==1)
           return;
       int i;
       for(i=2;i<=n;i++)
           if(n%i==0)
               {
                   System.out.println("Prime factor = "+i);
                   primefact(n/i);
                   break;
               }
   }
}