Help anyone?

Write a program that prompts the user to enter a positive integer between 1 and 1000 (inclusive) and that outputs whether the number is prime.

If the number is not prime, then output all the numbers, from the list of the first 11 prime integers, which divide the number.

#include <iostream>

using namespace std;

int main() {
// Write your main here
int i, n;
bool isPrime = true;
double x;
cout << "Enter a positive integer between 1 and 1000: ";
cin >> n;

if (1 == isPrime) {
isPrime = true;
}
else {
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
cout << n << " is a prime number";
else
cout << n << " is not a prime number.";
if (n != isPrime) {n == x;}
if (x/2 == isPrime) {cout << x/2 << " is divisibal by 2.";}



return 0;
}
I am trying to figure this out, but I am having trouble understanding the coding to divide the non-prime numbers by the 11 prime numbers listed above.
you can just hard code that part.
int p11[] {2,3,5,7,11,13,17,19,23,29,31};
for( everything in p11)
if num% p11[index] == 0 then it divides into it, print p[11] into your output list of factors.

you can also get mathy at it and multiply everything in p11 together into one integer and take the gcd (c++ has a gcd function, do not write it) in a while loop until you don't get any more factors (after finding a factor, divide both sides by it). Remember that factors can repeat: 8 is 2*2*2 when you do the prime factorization.

for (i = 2; i <= n / 2; ++i) {
^^ that can be only to sqrt n, not n/2. This is very significant for large n, but for large n, there are better ways to do it.

please use code tags. Is hard to read without. <> on the edit bar
Last edited on
I am a little confused about what you want me to change.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main() {
    // Write your main here
    int n;
    int i[] = {2,3,5,7,11,13,17,19,23,29,31};
    bool isPrime = true;

cout << "Enter a positive integer between 1 and 1000: ";
cin >> n;

if (1 == isPrime) {
isPrime = true;
}
else {
for (2,3,5,7,11,13,17,19,23,29,31) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
cout << n << " is a prime number";
else
cout << n << " is not a prime number.";

return 0;
}

This is what I think you wanted me to do, but it is not working.
for loops look like this:
1
2
3
4
5
6
7
8
for(int dx = 0; dx < 11; dx++) //traditional for loop
if(n%i[dx] ==0)  //use the array's values
{
 isprime = false; 
//or is the point to say which of the first 11 primes is a factor?
 cout << i[dx] << ", ";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>

using namespace std;

bool is_prime(int n)
{
    if( n == 1)
        return false;
    else
    {
        for( int i = 2; i * i <= n; i++)
        {
            if(n % i == 0)
                return false;
        }
    }
    return true;
}

int main()
{
    int chosen_no = 0;
    bool isPrime = false;

    // Enter a positive integer between 1 and 1000 (inclusive)
    while(chosen_no < 1 || chosen_no > 1000)
    {
        cout << "Enter a positive integer between 1 and 1000: ";
        cin >> chosen_no;
    }

    // Output whether the number is prime
    if( chosen_no == 1)
        isPrime = false;
    else
    {
        isPrime = true;
        for( int i = 2; i*i < chosen_no; i++)
        {
            if(chosen_no % i == 0)
            {
                isPrime = false;
                break;
            }
        }
    }

    cout << chosen_no;
    if(isPrime == true)
        cout << " is";
    else
        cout << " is not";
    cout << " prime\n";

    // Output first 11 primes and put in array
    const int NO_OF_PRIMES{11};
    int count{0};
    int first_11_primes[NO_OF_PRIMES]{0};

    for(int i = 1; i <= 1000; i++)
    {
        if(is_prime(i))
        {
            first_11_primes[count] = i;
            count++;
        }
        if(count > NO_OF_PRIMES)
            break;
    }

    for(int i = 0; i < NO_OF_PRIMES; i++)
    cout << i + 1 << '\t' << first_11_primes[i] << '\n';

    // If the number is not prime, then output all the numbers
    // from the list of the first 11 prime integers, which divide the number
    if( !is_prime(chosen_no) )
    {
        cout << chosen_no;
        for(int i = 0; i < NO_OF_PRIMES; i++)
        {
            if(chosen_no % first_11_primes[i] == 0)
            {
                cout << " is divisible by " << first_11_primes[i] << '\n';
            }
        }
    }
    return 0;
}


Enter a positive integer between 1 and 1000: 899
899 is not prime
1	2
2	3
3	5
4	7
5	11
6	13
7	17
8	19
9	23
10	29
11	31
899 is divisible by 29
 is divisible by 31
Program ended with exit code: 0
Thank you for your help. I still don't completely understand how I accomplished it, but I did.
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

int main()
{
	const int primes[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};

	int chose {};

	while (chose < 1 || chose > 1000) {
		std::cout << "Enter a positive integer between 1 and 1000: ";
		std::cin >> chose;
	}

	bool isp {true};

	for (const int p : primes)
		if (p != chose)
			if ((chose % p) == 0) {
				if (isp)
					std::cout << chose << " is not prime\n";

				std::cout << "It is divisible by " << p << '\n';
				isp = false;
			}

	if (isp)
		std::cout << chose << " is prime\n";
}



Enter a positive integer between 1 and 1000: 105
105 is not prime.
It is divisible by 3
It is divisible by 5
It is divisible by 7

Enter a positive integer between 1 and 1000: 19
19 is prime

Topic archived. No new replies allowed.