Just iterate in the increasing order from 2 to n-1 assuming n is the number to be checked and in each iteration check
- if iteration is the factor of the n and is prime
- if above condition is met then push the number in the stack else continue
- After the loop is over just pop everything from the stack and present.
PS: Your example is wrong 450 has prime factor as 5, 3, 2 and 1. 4 is not a prime number.
Following is the code (tested one)
using namespace std;
bool isprime(int number)
for (i=2; i<number; i++)
if (number % i == 0)
int number=450, halfNumber;
if((number % 2) == 0)
for (int i = 3; i < number; i++)
if ((number % i == 0) && (isprime(i)))
cout << "\nThe prime factors of " << number << " are: ";
cout << smallPrimeFac.top() << " ";