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!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
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;
}
|