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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int num1, sum1, numcheck = 0, primecheck = 0, perfcheck = 0, divider, repeat = 1, i ;
int primcounter = 0, perfcounter = 0; //primcounter for prime cycles. perfcounter for perfect cycles
//good god this is going to hurt
cout << "Enter a number into this program and it will calculate whether it is a prime " << endl;
cout << "number or perfect number." << endl;
cout << "Please use only positive numbers, as it is impossible to have a negative prime" << endl;
cout << "or positive number" << endl;
cout << "If you try to enter a negative number, you will be prompted to try again" << endl;
cout << endl;
while (repeat == 1)
{
repeat = 0;
while (numcheck == 0)
{
cout << "Please enter an integer between 1 - 1000, that you wish to be processed." << endl;
cin >> num1;
cout << endl;
if (num1 <= 0)
{
if (num1 == 0)
{
cout << "You have entered the number zero. This is an invalid entry. Please try again" << endl;
}
else
{
cout << "You have entered a negative number. Please try again" << endl;
cout << "Please remember, prime and perfect numbers can ONLY be positive" << endl;
}
}
else
{
if (num1 > 1000)
{
cout << "You have entered an integer above the stated threshold. Your inputed interger was " << num1 << endl;
cout << "Please re-enter your selection again, choosing a number between 1 - 1000" << endl;
}
else
{
cout << "Thank you!" << endl;
numcheck = 1;
}
}
}//this is the end of the NumberCheck to ensure the user has inputted a positive number
//check if a number is prime, by seeing total remainders when divided between 1 and the inputed number. If total == 2, then the number is prime. If more, it is not prime
for (int i = 2; i < num1; i++)
{
if (num1%i == 0)
{
primcounter++;
}
}
if (primcounter == 0)
{
cout << "The number " << num1 <<" is a prime number!" << endl;
primecheck = 1;
cout << "Would you like to check another number? Enter 1 to repeat, 0 to exit" << endl;
cin >> repeat;
}
else
{
cout << "The number " << num1 << " is a not prime number!" << endl;
sum1 = 0;
for (i = 1; i < num1; i++)
{
if (num1%i == 0)
{
sum1 = sum1 + i;
}
}
if (sum1 == num1)
{
cout << "The number " << num1 << " is a perfect number" << endl;
cout << "Would you like to check another number? Enter 1 to repeat, 0 to exit" << endl;
cin >> repeat;
}
else
{
cout << "The number " << num1 << " is a perfect number" << endl;
cout << "Would you like to check another number? Enter 1 to repeat, 0 to exit" << endl;
cin >> repeat;
}
}
primecheck = 0;
primcounter = 0;
numcheck = 0;
perfcheck = 0;
i = 0;
}
system("pause");
}
|