Write a Java application that asks for an integer and return

Write a Java application that asks for an integer and returns its factorization into prime factors including their powers. The prime factors must appear in increasing order. For example if 120 (= 2*2*2*3*5) in the input number, then your program should output 120 = 2^3 * 3^1 * 5^1 The factors should appear in increasing order and should be separated from each other by asterisks (*).

Solution

import java.util.Scanner;


public class factorialFactorization {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner scan = new Scanner(System.in);
       System.out.print(\"Enter a number:\");
       int n = scan.nextInt();
       int fact=1;
       for(int i=2;i<=n;i++)
       {
           fact = fact*i;
       }
       int count=0,hat=0;
       System.out.print(fact);
       System.out.print(\" = \");
       for(int i=2;fact>1;i++)
       {
           count = 0;
           while(true)
           {
               if(fact%i==0)
               {
                   count++;
                   fact = fact/i;
               }
               else
               {
                   break;  
               }
           }
          
           if(count>0)
           {
               if(hat>0)
               {
                   System.out.print(\" * \");
               }
               System.out.print(i);
               System.out.print(\"^\");
               System.out.print(count);
               hat ++;
           }
       }
   }

}

 Write a Java application that asks for an integer and returns its factorization into prime factors including their powers. The prime factors must appear in inc

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site