can anyone plz help me with this :/
i have to 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.
i wrote the codes but every time i enter two integers it gives me the numbers between those two integers and i dont know i what i did wrong. plz help
# include <iostream>
usingnamespace std;
bool isPrime(int number);
int main()
{
int m;
int n;
int number;
cout << "Enter two integers m and n, m < n:";
cin >> m;
cin >> n;
cout << endl;
cout << " The primes between " << m << " and " << n << " are: ";
for (int i = m; i <= n; i++ )
{
if (isPrime(i))
cout << " "<< i << " ";
}
return 0;
}
bool isPrime(int number)
{
for (int i=2; i<number; i++)
if (number % i == 0)
returnfalse;
elsereturntrue;
}
for (int i=2; i<number; i++)
if (number % i == 0)
returnfalse;
elsereturntrue;
If your number is not evenly divisible by i, the function immediately returns true. So for odd numbers which are not prime, the function will return true the first time the loop runs and i == 2.
What it should look like is this:
1 2 3 4 5 6 7 8 9 10 11
bool isPrime(int number)
{
for (int i=2; i<number; i++)
{
if (number % i == 0)
returnfalse;
}
returntrue;
}
by placing the returntrue statement after the loop, you ensure that the function will not return true unless none of the numbers tested can evenly divide the input.
cout << "Enter two integers m and n, m < n:"<<endl;
cout<< "Enter m: ";
cin >> m;
cout<< "Enter n: ";
cin >> n;
cout << endl;
cout << " The primes between " << m << " and " << n << " are: ";
for (int i = m; i <= n; i++ )
{
if ((i==2||i==3||i==5||i==7)||(!(i%2==0||i%3==0||i%5==0||i%7==0)))
cout << " "<< i << " ";
}