prime numbers

hi i am not sure what is wrong with the second part of the code. can someone please help what wrong with it.

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
// Program to test if an integer is a prime number

#include <iostream>
#include <fstream>

using namespace std;

int genprimes(int, int);
int i1 =10000;
int i2 =11000;
int a;
int b;

int main()
{
	int n;

	cout << "Enter an integer please" <<endl;
	cin >> n;

	if(n==1)
	{
		cout<< n << " is not a prime number" <<endl; 

	}

	if(n==2)
	{
		cout<< n << " is a prime number" <<endl; 

	}

	for(int i=2; i<n; i++)
	{
		if (n%i==0 )

        cout << n << " is not a prime number" << endl;
 
        else if (n%i!=0)
        
		cout << n << " is prime number" << endl;
	

		return 0;
	}
}




int genprimes(int i1, int i2)
{


    int a=i1;
	int b;

    ofstream outfile;
    outfile.open ("prime.dat");

    for(a; a<=i2; a++)
    {

	  if(a%!=0 )
	  {
		outfile << "b" << endl;
		
	  }
	  else 
	  {
		outfile << "Invalid" <<endl;
	  }

	    outfile.close();
     }


return 0;

}

that code is really messy
First of all, you can generally assume, that if you tried to divide nubmer N by all numbers from 2 to sqrt(N), then it is prime(because no number can have prime factor larger then its square).

So, your prime test should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool isPrime(int number)
{
 bool prime = true;
 if(number == 1)
    prime = false;
 else
 {
   for(int i = 2; i < number; ++i)
      {
           if(number % i == 0)
           {
               prime = false;
               break;
           }
      }
 }
}


Now back to your question. First of all, you already have two integers - i1 and i2. You don't have to create int a and b and assign i1 or i2 to them. Especially, that a could be ignored and i1 could be used instead, and b isn't used at all.

Also, for loop declaration isn't good. It should be:
1
2
for(;a <=i2; ++a)
//code... 

Yet it is unclear to me what i2 and i1 would be, since you have never commented your code, or used the function.
Output also looks a bit strange to me, but your first if will not work:
 
if(a%!=0)//What is it supposed to do!? 


And why make a function return an int, when it always returns 0?

Cheers!

PS. I agree with SorinAlex. I would recommend you to:
a) Divide your code into:
- a function that will return bool, and take an integer as an arument, and check if argument is prime(basically the function I wrote above).
- A function that will write to file
- Main function, combining both functions
b)Comment your code - tell us what your code is doing, or is supposed to do. :)

Good luck!
Last edited on
Thanks for the advice. Im only a beginner so that is why my code is not great. The second part is meant to get all of the prime numbers between 10000 and 11000 and put them into a file
So, to improve your code:
- don't use global variables
- try to use std:: prefix instead of using namespace std;. Eventually, you can use using std::cout;, or cin, or anything else. It helps you to avoid naming collision, and is considered nice practice.
- Try to write comments to your code(not necessary to really simple one, and don't re-write code in comments; code should explain why something works if that's not clear, or what something does.


Back to code. First - please, fix the things I mentioned, or answer me if these are supposed to be left as they are. Secondly, tell us what doesn't work - compilation errors, run-time errors? :)
Also, you probably wanted to do this:
1
2
3
4
	  if(/*number is prime*/ )
	  {
		outfile << b << endl;//No "". You want to print the prime number, not "b" character.
	  }

And no else - if you would check 1000 numbers if they are prime, and only 100 were prime, then you would have 100 primes scattered in 1000 lines of code, between "Invalid", which doesn't say too much. It's much better, imho, to just save primes. All mentioned there are primes :)

Tell us if you solved problem or still need help.
And your code will get better with time :)

Cheers!
Thanks for the help. I still cannot get the numbers to the file but the code works as there is no errors. I changed the code to print it out on the complier and it prints out all the prime numbers. Why will it not print out into the file? Heres my code.

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
// Program to test if an integer is a prime number

#include <iostream>
#include <fstream>

using namespace std;


int i1 =10000;
int i2 =11000;


int main()
{
	int n;

	cout << "Enter an integer please" <<endl;
	cin >> n;

	if(n==1)
	{
		cout<< n << " is not a prime number" <<endl; 

	}

	if(n==2)
	{
		cout<< n << " is a prime number" <<endl; 

	}

	for(int i=2; i<n; i++)
	{
		if (n%i==0 )

        cout << n << " is not a prime number" << endl;
		
	
 
        else if (n%i!=0)
        
		cout << n << " is prime number" << endl;
		break;
	}

	
	int i1 =10000;
    int i2 =11000;


    ofstream outFile;
    outFile.open ("prime.dat");


    for(int i1= 10000; i1<=11000; i1++)
    {
		for (int j=2; j<=(i1-1); j++)
        {
            if ((i1 % j) == 0)
				break;
		

            if ((i1-1)==j)

            outFile<< i1 <<endl;
			outFile.close();
		}
	}
		

       return 0;

}
Topic archived. No new replies allowed.