I'm working on a program that lists a numbers prime factors.
I've finished the bulk of the work and all I have left is the first print where it says the number is prime or composite. I know the way I'm going at it is wrong because it wont work with an if or while.
int x;
int prime = 0;
cout << "Please enter a number: ";
cin >> x;
for (int i=2; i <= x; i++){
if(x % i == 0){
prime = 0;
}
else{
prime = 1;
}
}
if(prime = 1){
cout << x << " is a PRIME number." << endl;
}
else{
cout << x << " is a COMPOSITE number." << endl;
}
I've looked at it and realize that no matter what I do no number is divisible by one less than it and because that is the last number in my loop it's always set to 1..
But, any suggestions on how to fix this?