Hello, I am having a problem with my program that finds the prime factorization of a number. I seem to be getting the correct prime factors, but my problem is with repeating factors. For example, if I input 3,428, the correct prime factorization is 2 x 2 x 857, but my program will only output 2 one time. Any hint on what I could try? Thanks in advance!
#include <iostream>
using namespace std;
void prime_num(int);
int main()
{
int num = 0;
cin >> num;
prime_num(num);
system ("pause");
}
void prime_num( int num)
{
bool isPrime=true;
for ( int i = 0; i <= num; i++)
{
for ( int j = 2; j <= num; j++)
{
if ( i!=j && i % j == 0 )
{
isPrime=false;
break;
}
}
if (isPrime)
{
if (num % i == 0)
{cout << i << endl;
//I thought that by dividing num by i it would loop back and divide by 2 again, but it didn't. Is there something I am missing?
num = num / i;
}
else
{i++;}
}
isPrime=true;
}
}