title: How to find prime factors of a number in c++?
body: <p>I am attempting project euler question number 3, and I don't get the desired result. My logic:</p>
  
  <ol>
  <li>List all the factors of the number 13195 and save them in an array.</li>
  <li>Check if each number in the array is a prime.</li>
  <li>If the number is found to be prime save it in an other array.</li>
  <li>display the contents of the second array.</li>
  <li>Hope it contains only prime factors.</li>
  </ol>
  
  <p>RESULT: The first array contains all the factors as expected, The second I think duplicates the first array or slips in some non-primes, Please help! :)</p>
  
  <p>My code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  long int x,y=2;
  long int number=13195;
  long int f[1000000],ff[1000000];
  int st=1;
  int open=0;
  int open2=0;
  int a=0;
  bool isprime;
  
  int main()
  {
  
      for(x=1;x&lt;=number;x++)
      {
          if(number%x==0)
          {
              f[a] = x;
              a++;
          }
      }
      while(st&lt;=16)
      {
          while(y&lt;f[st])
          {
              if(f[st]%y==0 &amp;&amp; f[st]!=y)
              {
                  break;
              }
              else if(f[st]%y!=0 &amp;&amp; f[st!=y])
              {
                  ff[open] = f[st];
              }
              y++;
          }
          open++;
          st++;
      }
      for(open2=0;open2&lt;open;open2++)
      {
          cout&lt;&lt;ff[open2]&lt;&lt;" is a prime factor of "&lt;&lt;number&lt;&lt;"\n";
      }
      return 0;
  }
  </code></pre>
  
  <p>using this for finding the prime works:</p>
  
  <pre><code>while(st&lt;=a){
      int k = f[open];
      for(int i=2;i&lt;k;i++)
      {
          if(k%i==0)
          {
              isprime = false;
              break;
          }
          else if(f[open]!=0 &amp;&amp; f[open]%i!=0 &amp;&amp; f[open]!=i)
          {
              isprime =true;
          }
  
      }
      if(isprime==true)
      {
          ff[st] = k;
          open3++;
          isprime = false;
      }
      open++;
      st++;
      }
      cout&lt;&lt;"The primes of them are "&lt;&lt;open3&lt;&lt;"."&lt;&lt;"\n";
      cout&lt;&lt;"Press RETURN to show them."&lt;&lt;"\n";
      cin.get();
      for(open2=0;open2&lt;=open3;open2++)
      {
          cout&lt;&lt;ff[open2]&lt;&lt;" is a prime factor of "&lt;&lt;number&lt;&lt;"."&lt;&lt;"\n";
      }
  </code></pre>