Help with factorization problem?

October 6, 2010 at 11:10:35
Specs: Windows 7
Using while loops - No bool. My program always divides by zero.. Any help?

#include <iostream>
using namespace std;

int main()
{
int value;
int leftToFactor;
int factor;

cout << "Please input a value to factor: ";
cin >> value;

while (value > 1) {
leftToFactor = value;
factor = 2;
while (factor <= leftToFactor){
while ((factor % leftToFactor) == 0){
cout << factor;
leftToFactor = leftToFactor / factor;

}
factor++;
}
cin >> value;
}

return 0;
}


See More: Help with factorization problem?

Report •


#1
October 7, 2010 at 08:19:18
while ((factor % leftToFactor) == 0){
cout << factor;
leftToFactor = leftToFactor / factor;
}

I'm not sure what your logic is, but this loop is your problem; sooner or later, leftToFactor will be 0, and that'll cause a divide by 0 error.

How To Ask Questions The Smart Way


Report •

#2
October 7, 2010 at 08:48:40
I had to reverse them, I eventually figured it out.. thanks though

Report •

Related Solutions


Ask Question