I have to turn in a project in my beginer C++ class in two days time and I am totally stuck. The project is to promt a user for an integer (between 1>=num<1000); Tell if that number is a prime number ( if not find a way to show the divisors of the number); and then if the number is prime,decide if it is a perfect number or not (once again showing the divisors that prove the number is perfect.

My teacher treats us like we rogram already and left me in a fog... I have very limited info on for-loops which is how we are to solve this program.
so far i have to find if a number is prime or not...

#include <iostream>
#include <string>

using namespace std;

int main()

{

	double num;
	bool prime;

	cout<<"This is my program check if a number is a Prime number, also a Perfect Number!"<<endl;
	cout<<"Enter a number between 1 and 1000 "<<endl;
	cin>>num;
	if ((num<=0)||(num>1000))//SETTING UP THE FIRST ERROR MESSAGE
	{
			cout<<"Error, Please re-enter a valid number between 1 and 1000!"<<endl;
	}
	//NOW I AM GOING TO START THE MATH!

	for (int i = 3; i <= num; i++){
		prime=true;

		for (int num = 2; num <= i - 1; num++){
			if (i%num==0){
				prime=false;
			}
		}
		if (prime){
			cout<<num<<" is a Prime number!"<<endl;
		}
	}

	return 0;
}//End of Program

This just list out the prime numbers though... I cannot figure out how to make it show the divisors for the number....

an for perfect numbers is it

for (int i=1; i <= ( n / 2 ): i++ )

if (num%i==0)
product += i;
prime=true;

I am trying but I am afraid I am out to sea with no paddle...Teacher wont provide insiht, So I turn to you for some direction and guidance.
Thank You
D

Recommended Answers

All 3 Replies

#include <iostream>
#include <string>

using namespace std;

int main()

{

	double num;
	bool prime;

	cout<<"This is my program check if a number is a Prime number, also a Perfect Number!"<<endl;
	cout<<"Enter a number between 1 and 1000 "<<endl;
	cin>>num;
	if ((num<=0)||(num>1000))//SETTING UP THE FIRST ERROR MESSAGE
	{
			cout<<"Error, Please re-enter a valid number between 1 and 1000!"<<endl;
	}
	//NOW I AM GOING TO START THE MATH!

	for (int i = 3; i <= num; i++){
		prime=true;

		for (int num = 2; num <= i - 1; num++){
			if (i%num==0){
				prime=false;
			}
		}
		if (prime){
			cout<<num<<" is a Prime number!"<<endl;
		}
	}

	return 0;
}//End of Program

One, use better variable names. The number you are checking is named num. You then use num as the name for your loop variable in line 25. It's confusing to have two variables of the same name. Second, you have the first num declared as a double. Primality and factoring only makes sense for integers. Have the user enter an integer and store it as an integer. Also, the user should enter a number greater than or equal to 2. Primality makes no sense for 0 or 1. If you have to check 0 or 1, just test the number and it it's 0 or 1, declare it as non-prime and bail out of the program without more testing.

Thanks VernonDozer... I wish my teacher wasn't a programing dinosour that worked at IBM in the 80's, and now knows everything. :) She means well, but not the best professor. Any advice is helpful and thanks again, hopefully I can pull this off.

by any chance her name is Sue Krimm?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.