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

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.