printing all prime numbers between two numbers

Can anyone help me with this question please

Write a program that prompts the user to enter two positive integers m and n where m < n then output all prime numbers in the range m and n, inclusive. You must write a function that would test a number whether it is prime.

thanks in advance
Last edited on
First you need to try to complete the exercise on your own.

Then when you fail, you will be able to ask specific questions and get specific help, because you will know what exactly you don't know.

This process is called "learning".
However if you don't want to learn, you can ask again for us to write a full solution, and you will gain very little in knowledge. (Good teachers will see this quickly.)
sorry but it's my first time here


ok I'll post what I did

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

#include <iostream>


using namespace std;
bool isPrime(int);
int main()
{
 int m,n,i;
 cin>>m>>n;

for( i=m ; i<n ; i++)
{

    if (isPrime)
   cout<<" "<<i<<" ";

else
break;

}

bool isPrime(int N);
{
int N;
    if(N%2==0)
    return false;

    else

    return true;


}






return 0;


}
It's printing all numbers starting from m to the number before n

but I want only prime numbers
To begin, your code looks horrible.
It looks horrible because of your inexperienced and disordered style of formatting.

This problem must be addressed first, so I will beautify your code.
Hopefully you could try to adopt this style for yourself.

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

bool isPrime(int);

int main()
{
    int m, n, i;

    cin >> m >> n;

    for(i = m; i < n; i++)
    {
        if (isPrime)
            cout << " " << i << " ";

        else
            break;
    }

    bool isPrime(int N);

    {
        int N;

        if (N % 2 == 0)
            return false;
        else
            return true;
    }

    return 0;
}


Now it is easier to see some of the things that are wrong with the program.

You do not have a isPrime() function. On lines 4 and 21 you declare it, but you don't define it.

Line 21 contains a declaration because the function's name is followed by a semicolon ;.

So what starts at line 23 is not a body for a function, it is just a block inside the main() function. You can have as many blocks as you want.

1
2
3
4
5
6
7
8
int main()
{
    {
        {
            int i;
        }
    }
}


However, you may not define a function inside another function, so your isPrime() must be written outside main().

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

bool isPrime(int);

int main()
{
    int m, n, i;

    cin >> m >> n;

    for(i = m; i < n; i++)
    {
        if (isPrime)
            cout << " " << i << " ";
        else
            break;
    }

    return 0;
}

bool isPrime(int N) // removed ;
{                   // now we have a body!
    // int N; // why does this local variable exist?

    if (N % 2 == 0)
        return false;
    else
        return true;
}


Now we must fix the isPrime() function.
How do you check if a number is prime? It is not enough that you check if it divides exactly by 2 (which is what you do right now).

You need to divide N by 2, 3, 4, 5, 6 and so on, until a certain number. Which is ...?

If you can tell me what is that certain number, I'll help you further.
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

#include <iostream>

using namespace std;

bool isPrime(int);

int main()
{
    int m, n, i;
    
    cin >> m >> n;

    for(i = m; i < n; i++)
    {
        if (isPrime)
            cout << " " << i << " ";
        else
            break;
    }

    return 0;
}

bool isPrime(int N)
{


for(int i=2; i<N ; i++)
    if (N % i==0)
        return false;
    else
        return true;
}


yeah so we should keep dividing it until we reach the number *N*

but still it is printing all numbers between n and m
To find a prime you must check if it is divisible by all the previous primes.

or

You can check if it is divisible by 2.
Then loop from 3 -> sqrt(n) incrementing by 2
Then every 3rd increment you can increment by an additional 2

This will greatly increase the speed and remove a bunch of unnecessary checks.

Though the fastest way would be to make a list of primes as you call the function then check those and it would have even less unnecessary loops.
yeah so we should keep dividing it until we reach the number *N*

You are correct.

but still it is printing all numbers between n and m

This is because there is a small mistake in your program, on line 16: you are not actually using isPrime().

16
17
18
19
20
21
22
        if (isPrime) // what does this do?
// it takes the memory address of the isPrime() function
// and checks if it's zero (which it isn't)
// so the result will always be true, and you're never calling isPrime()

        // the correction:
        if (isPrime(i)) // call the isPrime() function and pass `i' as paramater 

Aha ok ! I forgot it

Thanks for the help


now It's printing prime numbers

last question >> how can I make it print the number after m (without m) ?
last question >> how can I make it print the number after m (without m) ?

This is the last mistake we must fix. I'll explain and fix for you.

12
13
14
15
16
17
18
    for(i = m; i < n; i++)
    {
        if (isPrime(i)) // if i is prime...
            cout << " " << i << " "; // then print i
        else // otherwise if i is not prime
            break; // exit the for() loop!
    }


Obviously, you don't want to exit the for() loop with break.
So what to do if i is not prime?

Nothing! Don't do anything. Do we care about i if it isn't prime? No.
So remove the else branch completely.

12
13
14
15
16
    for(i = m; i < n; i++)
    {
        if (isPrime(i))
            cout << " " << i << " ";
    }


Edit: I see another small mistake. You want to go up to n inclusive, do you not? Then use:

for (i = m; i <= n; i++)
Last edited on
I dont want m to be printed niether n

I want only numbers between them

:/
i = m+1; i < n; ++i
Thaaaaanks :)
Topic archived. No new replies allowed.