Need to find prime numbers between two numbers

The program works, but it includes the two number I input. I have to find a way to stop the program from outputing the input 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
27
28
29
30
#include <iostream>
using namespace std;

int main(){
    int one;
    int two;
    
    cout << "Please enter two different numbers and I will tell you what numbers between are not prime numbers." << endl << endl;
    cout << "Enter your first number" << endl;
    cin >> one;
    cout << "Enter your second number" << endl;;
    cin >> two;
    
    while(one <= two){
        if(one%2 == 0){
            cout << one << " is not a prime number." << endl;
        }
        else if((one%3 == 0)&&(one!=3)){
            cout << one << " is not a prime number." << endl;
        }
        else if((one%5 == 0)&&(one!=5)){
            cout << one << " is not a prime number." << endl;
        }
        else if((one%7 == 0)&&(one!=7)){
            cout << one << " is not a prime number." << endl;
        }
        one++;
    }
    system("pause");
}

It's very simple.
replace lines 13 and line 14 with the following code:

1
2
one++;    // [line 13] (before while loop condition) You start by checking the number after the 'one'
while(one <= two-1) // [line 14] and you check until the number before 'two'  


This way, you don't include 'one' and 'two'

Hope this helps!
Last edited on
thank you so much
Hi Kevin,

Welcome to cplusplus :+)

I just wanted to mention about your algorithm, it is only going to work for numbers up to 49 (72), which is rather limited, and your code is not scalable. If one has a list of all the numbers, then removes multiples of all the numbers up to N, then the numbers up to N2 will be prime.

So rather than repeating your code for each number, try to think of a way of doing it using loops.

On the wiki page for prime numbers, at the end there is the Euler's method. Have a go at implementing that.

Good Luck! :+)
First you need to figure out which of the 2 numbers entered is the smallest.
Then start a loop for the smallest integer till the highest integer.
Second you need to remember what the conditions for a prime is.
A prime number is a number that has only two factors: 1 and itself.
You need to setup a for loop that checks for factors for each number in the range entered.

Also you should check if the numbers are the same and only display prime for that number.

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
#include <iostream>
using namespace std;

int main(){
    int one;
    int two;
int small;
int large;
int factorcount;
    
    cout << "Please enter two different numbers and I will tell you what numbers between are not prime numbers." << endl << endl;
    cout << "Enter your first number" << endl;
    cin >> one;
    cout << "Enter your second number" << endl;;
    cin >> two;

while(cin)
{
if (one == two)
{
if (one == 1 || one == 2 || one <= 0) // Integers must be:postive,non-zero,not "1" or "2."
        cout << number << " is not a prime." << endl << endl;
      else 
        {
          for (int i=1; i<=one; i++) // Determines factors
            if (one% i == 0)
              factorcount++;
          if ( factorcount == 2) // If factor are 2 then display prime
            cout << number << " is a prime." << endl << endl;
          else
            cout << number << " is not a prime." << endl << endl;
          factorcount = 0;
        }
}

if( one < two)
{
one = small;
two = large;
}
else 
{
two = small;
one = large;
} // this if, else loop will set the smallest integer as "small" and the larger integer as "large"

for (int i=small; i < large; i++)
{
if (i== 1 || i== 2 || i<= 0) // Integers must be:postive,non-zero,not "1" or "2."
        cout << i<< " is not a prime." << endl << endl;
      else 
        {
          for (int j=1; j<=i; j++) // Determines factors
            if (i% j == 0)
              factorcount++;
          if ( factorcount == 2) // If factor are 2 then display prime
            cout << number << " is a prime." << endl << endl;
          else
            cout << number << " is not a prime." << endl << endl;
          factorcount = 0;
        }

    }
    system("pause");
}


I didn't check brackets or anything; not sure if this will even compile.
Topic archived. No new replies allowed.