Hi,

I have an assignment that requests that two integers be provided in a range and that these numbers should be checked if they are prime or not.

I am having the following problems with my code.

a) the number 4 shows up in my calculation and four should not be prime.

b)the assignment specifies that the function testValue be converted to a double.
example where testValue (min, max) make testValue double or

double testValue (min, max)

c) the function is_prime should be stated with sqrt, instead of division. example

for (i = 2; i < sqrt(n); i++);

I began with division and it worked fine
example for ( i=2; i < n/2; i++);

can anyone help?

HERE IS MY CODE:

// This is tutorial 10.17 (Prime Numbers Application)
// Date - 11/11/2011

#include <iostream> // required to perform C++ stream I/O
#include <iomanip> // required to manipulate C++ stream I/O
#include <string> // required to access string functions.
#include <cmath> // required to perform math operations.
#include <windows.h> //will be used to perform a series of beeps that creates a sound byte.

using namespace std;

// Defines the function prototype for the range of numbers used.
void testValue( int& min, int& max);

// Defines function prototype for is_prime.

bool is_prime (int n); // boolean prototype determines if true or false.

int main()
{
int min = 0,
max = 0;

cout <<"\n**************************************************";
cout <<"\n>>>> Welcome to the Prime Numbers Application <<<<";
cout <<"\n**************************************************"<<endl<< endl;

// Range of numbers used > 2, max is not specified.

testValue(min, max); // <--------make this double -- double testValue (min, max)

// outputs the prime numbers.

cout << "\nThe Primes Numbers are:\n"; // displays the output.

for (int n = min; n <= max; n++)
{

if (is_prime(n))
{

cout << " " << n << endl;

}

}
cout << endl;

return 0;
}

// defines the range of numbers that will be determined as primes.

void testValue(int& min, int& max)

{
cout <<"\nEnter a lowerbound value: ";
cin >> min;

cout <<"\nEnter an upperbound value: ";
cin >> max;

while (min < 2 || max < 2 && min > max)

{
cout <<"\nPlease enter a number >= 2. Try again."<< endl << endl;
cout <<"Renter lowerbound value: ";
cin >> min;
cout <<"Renter upperbound value: ";
cin >> max;
}
}

// This is the function for is_prime.

bool is_prime(int n);
{

for (int i=2; i < double sqrt (double n); i++); //<--- make this sqrt ( works fine if i divide by n example for (int i=2; i < n/2; i++);

if (n%i==0)

return false;

return true;

}

Any help will be greatly appreciated.

try this:

``for (int i=2; i <= sqrt(n); i++)``
Be a part of the DaniWeb community

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