Need some help with my program that finds divisors and prime/perfect numbers

So, here's what I have...

As you can see by my code, if a number is prime, I just output 1 and that number. For whatever reason, I'm not getting the divisors, when it is not a prime number.

Also, when I'm finding out if a number is perfect, when trying out the perfect number, 496, I found that I outputted the following numbers: 1, 2, 4, 8, 16, 31, 62, 124, 248.

It should be outputting 16, 32, 64, etc. so I'm not sure what's wrong with my code! Thanks in advance!!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include using namespace std; int main() { int userNum = 0, sum = 0; cout << "\nThis program will take numbers entered by a user, and will determine if a number is a prime number or a perfect number." << endl; cout << "\nPlease enter a number between 1 and 1000: \n"; cin >> userNum; while (userNum < 1 || userNum > 1000){ cout << "\nPlease enter a number between 1 and 1000: \n"; cin >> userNum; } cout << "\nDivisors: \n"; if (userNum % 2 == 1){ cout << userNum%2 << " and " << userNum << endl; cout << "\nThis number is a prime number. \n"; } else{ for (int z = 1; z <= userNum/2; z++){ if (userNum % z == 0) cout << z << endl; cout << "\nThis number is not a prime number. \n"; } } cout << "\nDivisors: \n"; for (int i = 1; i <= userNum/2; i++){ if (userNum % i == 0) sum += i; cout << i << endl; } if (sum == userNum) cout << "\nThe number " << userNum << " is a perfect number. \n"; else cout << "\nThe number " << userNum << " is not a perfect number. \n"; return 0; }``````
Last edited on
496 / 64 = 7.75
496 / 32 = 15.5

That is, neither 32 nor 64 are factors of 496, so these should not be output.

2*2*2*2*31 = 496
1+2+4+8+16+31+62+124+248 = 496
Last edited on
Thanks, you're right. I still don't understand why my program does not state that it is a perfect number.

Also, don't get why the else statement on line 30 does not give me the divisors of the number.
You can probably adapt this to give the required output. Your code was almost correct, apart from the checking for prime numbers which was seriously flawed. I would normally give hints rather than a full solution, but I borrowed the function `isPrime()` from a current thread on the front page of this forum.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````#include #include using namespace std; bool isPrime(int num); int main() { int userNum = 496; int sum = 0; cout << userNum << endl; if (isPrime(userNum)) cout << " is prime" << endl; cout << "\nDivisors: \n"; for (int i = 1; i <= userNum/2; i++) { if (userNum % i == 0) { sum += i; cout << i << endl; } } if (sum == userNum) cout << "\nThe number " << userNum << " is a perfect number. \n"; else cout << "\nThe number " << userNum << " is not a perfect number. \n"; return 0; } bool isPrime(int num) { if (num == 1) return false; int root = sqrt((double)num); for (int x = 2; x <= root; x++) { if (num % x == 0) return false; } return true; }``````
Topic archived. No new replies allowed.