title: Prime Factors in JAVA
body: <p>I am trying to write a program that give an output as prime decomposition of a given number. However, my code gives correct answer as an output of <code>"2**2**2**2**2**5**7**7**11*"</code> but I want it specific output as <code>"(p1**n1)(p2**n2)...(pk**nk)"</code>. Here's my code:</p>
  
  <pre><code>public class PrimeDecomp
  {
      public static String factors(int n)
      {
          String ans = "";
          for (int i = 2; i &lt;= n; i++)
          {
              if (n % i == 0)
              {
                  // checks if i is a divisor of num
                  ans += i + "**";
                  // writes i in prime factorization
                  n = n/i;
                  // since it is written down, num=num/i
                  i--;
                  // just in case their are multiple factors of same number.
                  // For example, 12=2*2*3
              }
          }
          return (ans.substring(0, ans.length() - 1));
      }
  
      public static void main(String[] args)
      {
          System.out.println(PrimeDecomp.factors(86240));
      }
  }
  </code></pre>