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

Dani AI

Generated

correctly flagged the two main beginner pitfalls visible in 's post: a floating-point type for an integer problem and reusing the same name for different variables (shadowing). The following clarifies the goal and shows a compact, for-loop-based approach that (a) validates an integer 1..1000, (b) detects primality efficiently, (c) collects and prints proper divisors, and (d) checks for a perfect number.

Key ideas (keeps everything inside simple for-loops):

  • Treat the input as an int and handle 1 as a special non-prime case.
  • Test primality by trial division up to floor(sqrt(n)). Early exit once a divisor is found.
  • To list proper divisors and compute their sum, iterate i from 1 to floor(sqrt(n)). When i divides n, add i and the paired divisor n/i if it is different and not equal to n. Exclude n itself from the "proper divisors" sum.
  • Note: primes (>1) cannot be perfect because their proper-divisor sum is 1. Small perfect numbers <= 1000 are 6, 28, and 496 — useful test values.

Example C++ sketch (keeps variable names distinct and uses only for-loops):

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

int main() {
    int n;
    std::cout << "Enter integer (1..1000): ";
    if (!(std::cin >> n) || n < 1 || n > 1000) { std::cout << "Invalid input\n"; return 0; }

    bool isPrime = true;
    if (n <= 1) isPrime = false;
    for (int i = 2; i * i <= n && isPrime; ++i) if (n % i == 0) isPrime = false;

    std::vector<int> divisors;
    int sumProper = 0;
    for (int i = 1; i * i <= n; ++i) {
        if (n % i == 0) {
            int j = n / i;
            if (i != n) { divisors.push_back(i); sumProper += i; }
            if (j != i && j != n) { divisors.push_back(j); sumProper += j; }
        }
    }
    std::sort(divisors.begin(), divisors.end());

    if (isPrime) std::cout << n << " is prime\n";
    else {
        std::cout << n << " is composite; proper divisors:";
        for (size_t k = 0; k < divisors.size(); ++k) std::cout << ' ' << divisors[k];
        std::cout << '\n';
    }

    if (sumProper == n) std::cout << n << " is perfect\n";
    else std::cout << n << " is not perfect\n";

    return 0;
}

Troubleshooting notes: avoid reusing loop-variable names (no shadowing), do not use double for integer checks, remember to exclude n when summing proper divisors, and prefer sqrt(n) bounds for speed. Test with 1, 2, 6, 28, 496 and a large prime like 997 to confirm correctness.

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.