Primes between Limits

hey, I have been trying for a looong time to write a program to print out all the prime numbers between two given numbers. I have tried out alot of different methods, but none of them work. I have written my own program to check prime numbers (it is different from those provided by my teacher and my book, and all other sources..but it's similar..the concept is the same)

here is the prime number program:

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
#include<iostream.h>
#include<stdlib.h>


int main()
{
    int n, number, ctr; 
        
         cout<<"Enter the number ";
         cin>>n;
           if (n == 2)
              cout<<"The Number is a Prime";         
       /*    else if (n%2==0)
                       cout<<"The number is not a Prime";*/
         else 
         { ctr =2;
                        do
                        { number = n%ctr;
                          ++ctr;
                          
                          if (number == 0) 
                          
                           { cout<<"The Number isn't prime";
                            break;
                           }
                      //  cout<<"The number is "<<number;
                        }
                        while ( (ctr < n));
                          
                          if (number != 0)
                            {
                                cout<<"The Number is prime";
                            }     
                        
                        
        }

      cout<<endl<<endl;
         system("PAUSE");
         return 0;
      }
         



I tried to incorporate this program into the program i want to write (with the limits)...but I am unable to...my initial attempt was to create a loop within a loop..making the outer loop a do..while loop, and running that loop till "i", the prime number, reaches the max limit. But my use of the break function in the inner loop causes the program to break out of BOTH loops!!! (i have no idea why..?)

this is what I came up with now..to print out all the numbers in an array..but i don't know why it doesnt work...

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
#include<iostream.h>
int main()
{
    int array[25] , number, isprime, p_1, p_2,  i, num,ctr ;
        
        cout<<"Enter the minimum limit: ";
        cin>>p_1;
        cout<<"Enter the maximum limit: ";
        cin>>p_2;
  
  for (ctr = p_1 ; ctr<= p_2 ; ctr ++)
{
    isprime = i;
{    number = (i% ctr);
    if (number == 0)
        isprime = 0;
    else 
        isprime = i;
}

for (isprime= p_1 ; isprime<= p_2 ; isprime++)
    cout<<endl<<array[isprime];
  
}
   
        cout<<endl<<endl;
        system("PAUSE");
        return 0;
}               


I am limited to the knowledge of everything beyond loops...so I have to use them...please explain the concept to me..thanks, Ravish.
I would have a for loop going from min to max, in that loop have a nested loop that checks if the i in the loop can be %-ed (divided without getting remainder) with any below values, except for 1, if it can, it's not a prime.

Am I allowed to post sample code for this? Or is this a homework assignement?

Edit: I trust that you aren't doing a homework assignement.
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
#include <iostream>
#include <math.h>
using namespace std;

bool IsPrime(int Number)
{
	// Claim it's a prime
	bool _return = true;
	
	// Try to prove it's not. Don't check for 1 and the number itself
	for(int j = 2; j < sqrt((double)Number); j += 1)
	{
		// if it leaves a remainder in a modulo
		if(Number % j == 0)
		{
			// It's not a prime number
			_return = false;
		}
	}

	// Deliver the result
	return _return;
}
int main()
{
	int min = 10;
	int max = 100;
	cout << "Finding primes between " << min << " and " << max << "..." << endl << endl;
	
	// Check all primes between 10 and 100:
	for(int i = min; i <= max; i += 1)
	{
		//If it is a prime, print it
		if(IsPrime(i))
		{
			cout << "Found prime: " << i << endl;
		}
	}
	cout << endl;

	// Spesificlty check a single number
	if(IsPrime(4931))
	{
		cout << "4931 is a prime too" << endl;
	}

	cout << endl;
	system("pause");
	return 0;
}

There's no need in checking numbers above the square root of the one you're checking as it's only a waste of calculations.
Last edited on
I see, and I understand it now. (no, it is not for school or college, I am getting tutoring classes from somewhere, and this question wasn't assigned). Before now, I had been trying to do this question without using the bool function (because I am not very efficient at it yet..). But the program is probably impossible to do without the function.

thanks. Now I am going to modifiy what you wrote so that the program asks for the min and max (which isnt very hard).

--Ravish.
By bool function, you mean bool IsPrime(int Number)? You could have done without it and had the j-forloop nested in the one in main, but it's a good habbit to keep the code clear, simple and in cases lke this, reusable without needing to copy and paste the prime checking code.

To illustrate what I said, here's the code without the function that only lists the primes between and including min and max. Because you ain't gonna need a function if it is only used in the listing in only one place in your code. Also, I suggest you start using comments for other things than just removing bits of your code, because I couldn't exactly see how you thought in some places.
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
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
	int min = 10;
	int max = 100;
	cout << "Listing primes between " << min << " and " << max << "..." << endl << endl;
	
	// Check all primes between 10 and 100:
	for(int i = min; i <= max; i += 1)
	{
		// Claim it's a prime
		bool _isPrime = true;
		
		// Try to prove it's not 1 and the number itself
		for(int j = 2; j < sqrt((double)i); j += 1)
		{
			// if i/j leaves no remainder
			if(i % j == 0)
			{
				_isPrime = false;
			}
		}
		
		// If it's a prime -- state it so
		if(_isPrime)
		{
			cout << "Prime found: " << i << endl;
		}
	}

	cout << endl;
	system("pause");
	return 0;
}
Last edited on
Topic archived. No new replies allowed.