I'm kind of stumped on this one - I'm not sure what I'm doing wrong. Everything in my program is working fine, except for determining prime numbers. For the prime number section, if it's not prime I have an "else-if," but I had an "else" there fore a while, so I'm not sure that either of those make a difference.
#include <iostream>
usingnamespace 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. Enter -99 when you'd like to exit. " << endl;
do {
int userNum = 0, sum = 0;
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;
}
// Output divisors whether number is prime or not.
cout << "\nDivisors: \n";
for (int i = 1; i <= userNum; i++) {
if (userNum % i == 0) {
cout << i << endl;
}
}
for (int i = 1; i <= userNum; i++) {
if (userNum % i == 0) {
cout << "\nThis number is not a prime number. \n";
break;
} elseif ((userNum / 1 == userNum) && (userNum / userNum == 1)) {
cout << "\nThis number is a prime number. \n";
}
}
// Output divisors whether number is perfect or not.
cout << "\nDivisors: \n";
for (int i = 1; i <= userNum / 2; i++) {
if (userNum % i == 0) {
sum += i;
cout << i << endl;
}
}
// Check for a perfect number - The number must be equal to the sum of it's divisors. If 'k' is a divisor, it will be added up to sum.
if (sum == userNum)
cout << "\nThe number " << userNum << " is a perfect number. \n";
else
cout << "\nThe number " << userNum << " is not a perfect number. \n";
}
while (userNum != -99);
return 0;
}
for (int i = 1; i <= userNum; i++) {
if (userNum % i == 0) {
cout << "\nThis number is not a prime number. \n";
break;
} elseif ((userNum / 1 == userNum) && (userNum / userNum == 1)) {
cout << "\nThis number is a prime number. \n";
}
}
Now, the definition for a prime number is that it is only divisible by 1 and itself.
Every number, however, can be divided by 1 and itself, so checking for that in your loop doesn't make much sense. So really, all you need to do is the following:
1 2 3 4 5 6 7 8 9 10 11 12 13
bool prime = true;
for (int i = 1; i <= userNum; i++) {//edited as per Chervil's commentfor (int i = 2; i < userNum; i++) {
if (userNum % i == 0) {
prime=false;
break;
}
}
if (prime)
cout << "\nThis number is a prime number. \n";
else
cout << "\nThis number is not a prime number. \n";
Hope that helps, please do let us know if you have any further questions.
Hello, sorry for the delay - Was busy with other classes...Still not getting it. Maybe I missed something in my code. I always get that it's not a prime number, no matter what number I put.
#include <iostream>
usingnamespace 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. Enter -99 when you'd like to exit. " << endl;
do {
int userNum = 0, sum = 0;
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";
for (int i = 1; i <= userNum; i++) {
if (userNum % i == 0) {
cout << i << endl;
}
}
bool Prime = true;
for (int i = 1; i <= userNum; i++) {
if (userNum % i == 0) {
Prime = false;
break;
}
}
if (Prime)
cout << "\nThis number is a prime number. \n";
else
cout << "\nThis number is not a prime number. \n";
// Output divisors whether number is perfect or not.
cout << "\nDivisors: \n";
for (int i = 1; i <= userNum / 2; i++) {
if (userNum % i == 0) {
sum += i;
cout << i << endl;
}
}
// Check for a perfect number - The number must be equal to the sum of it's divisors. If 'k' is a divisor, it will be added up to sum.
if (sum == userNum)
cout << "\nThe number " << userNum << " is a perfect number. \n";
else
cout << "\nThe number " << userNum << " is not a perfect number. \n";
}
while (userNum != -99);
return 0;
}