I have an assignment that requires one to enter an integer, and then have the program say if it's either a prime number, or a perfect number, and it must show all the factors of every number that isn't prime.
So far, I have written this, but it always says the number is prime and not perfect(even when it is perfect), and then just tells me the factors.
It's also supposed to loop back to the beginning and ask for the user to re-input another number, but doesn't achieve that as well.
I've been trying to get this to work for hours.
Any help would be much much much appreciated. Thanks .
Code:
#include <iostream>
using namespace std;
int main ()
{
int n,i,sum=0;
bool notprime,perfect,isvalid;
char s='s';
cout << "Please enter a positive integer between 1-1000 to see if it is a prime"<< endl
<< "or perfect number, and what its factors are"<<
"Enter the s key to terminate the program";
cin>>n;
while (n!=s)
{
bool notprime=false;
for(i=2;i<n;i++)
{
if(n%i==0)
bool notprime=true;
break;
}
if(!notprime)
{
cout << "the number entered is prime";
}
else
bool perfect=false;
for(i=2;i<=n;i++)
{
if(n%i!=0)
continue;
sum+=n/i;
if(sum==n)
{ bool perfect=true;
break;
}
}
if(perfect)
{
cout<< "the number entered is perfect, and its factors are"<<endl;
for(i=1;i<=n;i++)
{
if(n%i==0)
cout << n/i <<endl;
}
}
else
{
cout << "The number entered is neither perfect nor prime, but its factors are";
for(i=1;i<=n;i++)
{
if(n%i==0)
cout << n/i <<endl;
}
}
cout << "Enter another number between 1-1000 to find if its prime or perfect";
cin>>n;
}
return 0;
}