Help with my code please (C++)

I need help making it so it prints prime numbers only. Thank you for your help in advanced.
-------------------------------------------
Example of how the code should look

Enter a value for N: 120

2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113

--------------------------------------------
This is what I have so far.

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
 void prime(int size){ 
    int array[size],count=0,j=2;
    bool isprime=true;
    for (int i=1; i<=size; i++){

        while(isprime==true && j<sqrt(i)){
            if(i%j!=0 && i!=j)
                isprime=false;
        }
        j++;
        if(isprime==true){
            array[count]=i;
            count++;
        }
        isprime=true;
    }
    cout<<"count: " <<count <<"\n";
    print(array,count);
}
void print(int primelist[],int length){
    for (int i=0; i<length;i++){
        if(i%10!=0)
            cout <<primelist[i] << " ";
        else
            cout<<endl <<primelist[i] << " ";
    }
    cout << endl;
} 
Last edited on
Prime number generators are all over the internet - just try a google search. Any question you could possibly have on this topic has already been answered a hundred times.

Please edit your post and make sure your code is [code]between code tags[/code] so that it has syntax highlighting and line numbers, as well as proper indentation.
Last edited on
I've looked on line and the only things that i keep getting is
__ is prime
OR
__ is not prime

However, that isn't what I am looking for
steph2015 wrote:
I need help making it so it prints prime numbers only.
steph2015 wrote:
However, that isn't what I am looking for
I do not understand what you are looking for.
I need it to print out like the example I have above. However I did my research before posting my question. I am not looking for an outcome that says "____ is prime" or "____ is not prime" or "number is prime" or "number is not prime"
You realize you don't have to print the exact same messages as the code you find online? You can print just the prime numbers, which is what you want to do.
You realize that if I knew how I wouldn't have asked and it would be done right now?

This file is under beginners for a reason. I'm a beginner at this. I'm new, which means I do not understand everything completely. I am still learning. I looked and I don't understand.

I would appreciate that if you will not help that you do not respond.
Last edited on
I am trying to help by pointing you to existing resources that can help you better than I can.

I still don't understand what specifically is confusing you. I understand the output you want, and I understand that you have done research. I can't help you directly unless you tell me specifically the thing that is confusing you.
Last edited on
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
 void get_input (int n)
{
    cout << "Enter a value for N: ";
    cin >> n;
    while (n>50000)
    {
        cout << "The value must be less than 50,000" << endl << "Enter a value for N: ";
        cin >> n;
    }
    find(n);
}
//--------------------------------------------
// finds count
void find (int m)
{
    int  count = 0, j=2;
    bool is_prime = true;
    for (int i = 1; i <=m;i++)
    {
        while (is_prime == true && j<sqrt(i))
        {
            if (i%j != 0 && i != j)
                is_prime = false;
        }
        j++;
        if (is_prime == true)
        {
            count ++;
        }
        is_prime = true;
    }
    int a[count];
    load (a,count);
}
//--------------------------------------------
//find prime
void load (int a[],int num)
{
    int j=2, count=0;
    bool is_prime = true;
    for (int i=1; i<=num;i++)
    {
        while (is_prime == true && j<sqrt(i))
        {
            if (i%j !=0 && i != j)
                is_prime = false;
        }
        j++;
        if (is_prime == true)
        {
            a[count] = i;
            count ++;
        }
        is_prime = true;
    }
    print(a,count);
}
//--------------------------------------------
//print prime
void print (int a[],int n)
{
    for (int i=0;i<n;i++)
    {
        if (i%10 != 0)
            cout<< a[i]<<" ";
        else
            cout << endl <<a[i] << " ";
    }

} 


updated code... I don't know how to reset j in order for the function to run.. Right now I keep getting
Input: 5
Output: 1 2 3 4 5

I want it to output: 2 3 5

Sorry it's VERY late for me and I'm just frustrated with this
Last edited on
Fun fact: 1 is neither prime nor composite.

You have a lot of different functions for one thing. Try condensing it into one function that prints the prime numbers from 2 to n. Loop through all the odd numbers from 2 to n and check if each one is prime - if it is, then print it.
Last edited on
do everything in main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main ()
{
    int n;
    cout << "enter number = ";
    cin >> n;

    for(int i=2; i<=n; i++)
    {
        //check if i is prime.
        //if prime, print it.
    }

return 0;
}
Here's a simple way to find prime numbers...

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
#include <iostream>

int main()
{
	int N (0);

        std::cout << "Enter a value for N: ";
	std::cin >> N;

	for ( int i = 2;  i <= N;  i++ )
	{
		bool isPrime = true;
		for ( int j = 2;  j < i;  j++)
		{
			if ( i % j == 0 )
			{
				isPrime = false;
				break;
			}
		}

		if (isPrime)
			std::cout << i <<  ' ';
	}
	return  0;
}

Happy coding...
Thank you everyone.
The only part I needed to add to my code for it to run correctly was
for (int j = 2; j<i ; j++ )
Topic archived. No new replies allowed.