hi, i've been working on this problem for way too long and i can't find the answer. here's my code:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int N; int M; int i;
    
    cout << "Input a positive integer: ";
    cin >> N;
    cout << endl;
    
    cout << "Testing 1 ->" << endl;
    cout << "   The divisors: 1" << endl << "   The number 1 is not a prime number." << endl << endl    ;
    if (N <= 0)
       cout << "Error: Please, enter a POSITIVE integer.\n";
    else
    {
        for (M = 2; M <= N; M++)
         {
            cout << "Testing " << M << " ->" << endl;
            cout << "   The divisors: ";
            for (i = 1; i <= N; i++)
            {
                if (M % i == 0)
                {
                      cout << i << " ";
                      if (i >= 2)
                            cout << endl << "   The number " << M << " is a prime number." << endl;
                      
                }
            }
            cout << endl;
        }
    
    
    }
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

the output is something like

Input a positive integer: 4

Testing 1 ->
The divisors: 1
The number 1 is not a prime number.

Testing 2 ->
The divisors: 1 2
The number 2 is a prime number.

Testing 3 ->
The divisors: 1 3
The number 3 is a prime number.

Testing 4 ->
The divisors: 1 2
The number 4 is a prime number.
4
The number 4 is a prime number.

Press any key to continue . . .


my problem here is that I don't know how to make the numbers that are not prime numbers cout something like "the number 4 is not a prime number" and have the divisors list like "The divisors: 1 2 4" instead of how it's shown.

any help is greatly appreciated

Dani AI

Generated

Quick diagnosis and a concise fix.

The observed duplicate/wrong messages happen because the primality test is performed inside the inner divisor loop and the test/flag isn't reset per candidate. That makes the program print the "prime" message once for every divisor >= 2 (so composites get the message repeatedly). Also the inner loop should test divisors up to the current number, not up to the overall N. As hinted, flipping a boolean can work — but the boolean must be initialized for each tested number and the final prime/not-prime decision must be printed after the divisor scan.

A simple, clear approach: collect divisors for the current candidate, print them, then decide primality once.

#include <iostream>
#include <vector>

int main() {
    int N;
    if (!(std::cin >> N)) return 0;
    for (int num = 2; num <= N; ++num) {
        std::vector<int> divs;
        for (int i = 1; i <= num; ++i)
            if (num % i == 0) divs.push_back(i);
        std::cout << "Testing " << num << " ->\n  Divisors:";
        for (int d : divs) std::cout << ' ' << d;
        std::cout << '\n';
        if (divs.size() == 2) std::cout << "  " << num << " is prime.\n\n";
        else std::cout << "  " << num << " is not prime.\n\n";
    }
}

Notes, performance and alternatives:

  • If only primality is needed (no divisor list), test i from 2 to sqrt(num) and exit as soon as a divisor is found — much faster.
  • For producing all primes up to N, the Sieve of Eratosthenes (as suggested) is the recommended algorithm.
  • Troubleshooting checklist: initialize any flags inside the per-number loop, print the result once (after the scan), and use correct loop bounds (i <= num). Also explicitly handle 1 and non-positive inputs.

This addresses the original symptoms reported by and ties back to advice from and while keeping the logic clear and easy to verify.

Recommended Answers

All 9 Replies

Use search box at the top right corner.
If you used it, then you would come with something like this

Use search box at the top right corner.
If you used it, then you would come with something like this

I did see that post. i just couldn't find where it shows how to do the non prime numbers like in my example. I'm sorry but today has been an extremely long and stressful day for me

Just do the opposite
something like, whenever he does if(isPrime) you change to if(!isPrime).
You might think of getting Juice and have a rest :)

lol i did this....

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int N; int M; int i;
    
    cout << "Input a positive integer: ";
    cin >> N;
    cout << endl;
    
    cout << "Testing 1 ->" << endl;
    cout << "   The divisors: 1" << endl << "   The number 1 is not a prime number." << endl << endl    ;
    if (N <= 0)
       cout << "Error: Please, enter a POSITIVE integer.\n";
    else
    {   bool isPrime=true;
        for (M = 2; M <= N; M++)
         {
            cout << "Testing " << M << " ->" << endl;
            cout << "   The divisors: ";
            for (i = 1; i <= N; i++)
            {
                if (M % i == 0)
                {
                      cout << i << " ";
                      if (i >= 2)
                            cout << endl << "   The number " << M << " is a prime number." << endl;
                      else if (!isPrime)
                            cout << endl << "   The number " << M << " is not a prime number." << endl;
                      
                }
            }
            cout << endl;
        }
    
    
    }
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

and got this:

Input a positive integer: 4

Testing 1 ->
The divisors: 1
The number 1 is not a prime number.

Testing 2 ->
The divisors: 1 2
The number 2 is a prime number.

Testing 3 ->
The divisors: 1 3
The number 3 is a prime number.

Testing 4 ->
The divisors: 1 2
The number 4 is a prime number.
4
The number 4 is a prime number.

Press any key to continue . . .


lol i am actually laying in bed but i'm too hard headed to leave this program without solving


lol i am actually laying in bed but i'm too hard headed to leave this program without solving

Mmmh, that is not very good. You debug program while tired and you use 5hrs with no success. You sleep and debug it next day, within 5minutes :)

yeah I know. i just need to set some code for the non prime numbers... blah i can't think

I am having trouble with the same problem...I can get the divisors part to work but I can't get the prime numbers part....if you figure it out let me know please.

when you are testing for prime numbers you are looking for factors of the number other than 1 and N. if you get up to N/2 without finding any then you consider it a prime. So you only have to modulus check with numbers up to N/2. if you find a number that is greater than 1 and divides perfectly e.g. modulus is 0. then you flag it as not a prime. and break out, if not you keep going... doing it this way is more computationally expensive. But who really cares, the computer is doing the work.

You can make it faster by using the previous prime numbers you found. to help you find the next one.

Alternatively you could use Sieve of Eratosthenes

I wrote this ages ago in C. but I imagine you could do it in C++.

#include <stdio.h>
#define MAX 10000
int main() {
	int i, j;
	int a[MAX];
	for (i=2; i<MAX; i++) {
		a[i] = 1;
	}
	for (i=2; i<MAX; i++) {
		if(a[i] == 1) {
			j =2*i;
			while (j < MAX) {
				a[j] = 0;
				j=j+i;
			}
		}
	}
	for (i=2; i < MAX; i++) {
		if (a[i] == 1) {
			printf("%d ", i);
		}
	}
}
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.