Hi!

I'm almost done with this program, but I have a problem: Whenever it displays the prime numbers, it throws in non prime numbers that are odd (like 9 and 15) Here is the code so far.

``````/*
File: isprime.cpp
Created by: Ricardo Renta
Creation Date: 11/8/11
Synopsis:
This program reads in a minimum and maximum integer greater than 1
and returns all primes between the minimum and maximum.
*/

#include <iostream>
#include <cmath>

using namespace std;

void read_range( int& imin, int& imax);

// FUNCTION PROTOTYPE FOR is_prime
bool is_prime (int k); //boolean prototype for true and false

// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{
int imin(0), imax(0);

// Print prime numbers
cout << "Primes:";
for (int k = imin; k <= imax; k++) {
if (is_prime(k))
{
cout << "  " << k;
}
}
cout << endl;

return 0;
}

{
cout << "Enter a minimum and maximum value:";
cin >> imin >> imax;

while (imin < 2 || imax < 2 && imin > imax)
{ cout << "The minimum and maximum values must be at least 2. Please try again."<< endl;
cin >> imin >> imax;
}
}

// DEFINE FUNCTION is_prime() HERE:
bool is_prime(int k)
{

for (int i=2; i<=k-1; i++)
{
if (k%i==0)
{return false;}

else
{return true;}
}

}``````

I know that the problem is in the function definition, specifically the for loop. Could I get a point in the right direction? Thank you in advance!

You need to put line 69-70 in line 72.

``````bool isPrime(int num){
for(int i = 2; i < num; ++i){
if(num % i == 0) return false;
}
return true;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.