Finding non prime numbers

The program is suppose to find the non prime numbers between two integers. It runs, but nothing comes out after i input the two integers

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;

int get_prime(int a, int b){
    //takes user input and loops through all number between a and b testing if they are not a prime
    while (a <= b) {
        if (a % 2 == 0 && a % 3 == 0 && a % 5 == 0 && a % 7 == 0) {
            cout << a << " is not a prime" << endl;
        }
        ++a;
    }
    
    
    return 0;
}

int main() {
    //set up variables to hold user input
    int num_1, num_2;
    //ask for Input
    cout << "Type two integers, I will display all prime numbers in between these two numbers" << endl;
    cout << "Press enter after typing each number" << endl;
    cout << "First Number: ";
    //take input
    cin >> num_1;
    cout << "Second Number: ";
    cin >> num_2;
    //call function get_prime passing input as parameters
    get_prime(num_1, num_2);
    cin.ignore();
    cin.get();
    return 0;
}
The program does nothing unless you have multiples of 210, for which it says that those numbers are not primes. Your condition is that it must be divisible by 2 AND 3 AND 5 AND 7 at the same time
You need a function that specifically returns whether or not a single number is prime:

1
2
3
4
bool is_prime( int n )
{
  ...
}

Your 'is prime' function will have to be a little more robust than checking if a number is not a multiple of 2, 3, 5, or 7. (For example, 11 is prime, but your current algorithm will not list it as such.)

Checking for prime numbers is not an obviously easy task. Does your professor expect you to figure out how to do it? Or can you just use code you found online?

(If you are permitted to use it, I can give you a piece of code that should suffice for your homework.)

The next step is to make sure that the numbers you get from the user are sorted, such that num_1 <= num_2.

Then all you need is a loop to go from num_1+1 to num_2-1, and check each one for primality using your 'is prime' function. If it is not prime, print it.

Hope this helps.

[edit] http://www.wikihow.com/Check-if-a-Number-Is-Prime [/edit]
Last edited on
Hi Kevin,

This now your 3rd post about the same question, just keep one post going. If you write some new code, then post that code in the same thread, don't start another.

It's a little annoying for those who reply, because they might write a long reply, only to discover that the same things have been said by someone else in the other thread, so they effectively wasted their time.

Anyway, there are lots of people willing to help, it's just that we would be a little bit happier if you could take on board this advice about how to post.

Regards :+)
Topic archived. No new replies allowed.