Hello people!
I am a beginner trying to teach myself C++. If you guys could help or suggest me something I would greatly appreciate it! Sorry for my English!
My program determines if the entered number is prime or perfect.
I have a few problems with it. I would really want to not use breaks or functions in this program. I have spent hours on it and I really would like it to work the way it is supposed to. :)
1) I have no idea why the program won't output the entered number in the prime spot when the number is not perfect nor prime.
This program will determine if your entered number is a perfect
or a prime number, and what are it's factors.
Please enter any positive number between 1 and 1000.
9
9 is not a perfect number.
is not a prime number.
Do you want to find the value of another character? (y/n)
n
Press any key to continue . . .
2) The program doesn't output the numbers that are not prime.
3) When I input a perfect number for a first time it says that it is a perfect number, but when I continue the program and input another perfect number without exiting the program, it says that is not a perfect number.
Here is an example, both 6 and 28 are perfect numbers.
This program will determine if your entered number is a perfect
or a prime number, and what are it's factors.
Please enter any positive number between 1 and 1000.
6
6 is a perfect number.
is not a prime number.
Do you want to find the value of another character? (y/n)
y
This program will determine if your entered number is a perfect
or a prime number, and what are it's factors.
Please enter any positive number between 1 and 1000.
28
28 is not a perfect number.
is not a prime number.
Do you want to find the value of another character? (y/n)
n
Press any key to continue . . .
4) I would like to output a list of divisors of non-prime numbers and a list of divisors of perfect numbers, and output about 5-10 of them per line, but I really have no idea how to do it.
Here is my source code.
Thank you! :)
#include <iostream>
#include <string>
using namespace std;
int num, i=1, sum=0;
char choice;
bool prime, perfect;
int main()
{
do{ // looping the program back to the beginning
cout << "This program will determine if your entered number is a perfect\n" <<
"or a prime number, and what are it's factors.\n\n" << // tells what the program does
"Please enter any positive number between 1 and 1000.\n"<< endl; // asking the user to input a number
cin >> num;
if (num >= 1 && num <= 1000) // checks if number is within the range
{
// This part determines wheter the entered number is a perfect number.
while(i<num) // checks if the i is smaller than the entered number
{
if(num%i==0) // checks if the entered number devided by i is equal to 0
sum=sum+i; // adds i to the sum
i++; // increments i by 1
}
if(sum == num) // checks if the sum is equal to the entered number
cout << "\n" << num << " is a perfect number." << endl; // message for a perfect number
else
cout << "\n" << num << " is not a perfect number." << endl; // message for a not perfect number
// This part determines wheter the entered number is a prime number.
prime = true; // starts as a prime number
if (num == 1) // 1 is not a prime
prime = false; // sets the prime flag to false
else if (num == 2) // 2 is a prime
prime = true; // sets the prime flag to true
else if (num % 2 == 0) // if it divides evenly by 2 it's not a prime
prime = false; // sets the prime flag to false
for (int x = 3; x<= (num/2); x+=2) // starts with 3 and increases by 2 every time
{ // because multiples of 2 are never prime
if (num % x == 0) // checks if the remainder is equal to 0
{
prime = false; // sets the prime flag to false
}
}
if(prime == true) // if the number is prime it outputs
{
cout << "\n" << num << " is a prime number." << "\n" << endl; // message for a prime number
}
if(prime == false) // if the number is not prime it outputs
{
cout << "\n" << " is not a prime number." << "\n" << endl; // message for a not prime number
}
}
else
{
cout << "\n" << "Number is not within range, please try again."<<endl; // error message for a number out of the range
}
cout << "\n" << "Do you want to find the value of another character? (y/n)\n" // message about restarting the program
<< endl;
cin >> choice; // inputing the choice character
}while(choice !='n'); // looping the program back to the beginning
system("pause");
return 0;
}