Toronto C++ User Group Message Board › Prime number test... help

Prime number test... help

Shivam S.
user 96266692
Toronto, ON
Post #: 1
Hey guys,
Ive been working on a software that enables user to find all prime numbers within a given range of numbers...
i've written the code! just cant figure out the mistake!!! any amendments will be appreciated!!

#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

bool prime (int n);

int main() {
int n1;
int n2;
int n;
cout <<"Enter the starting range and press ENTER:";
cin >> n1;
cout <<"Enter the ending range and press ENTER:";
cin >> n2;

for(n=n1; n<=n2; n++) {
prime(n);
while (true){
cout << n <<endl;
break;
}
}
system("pause");
return 0;
}

bool prime(int n) {
int i;
int n1;
int n2;
for (i=n1; i<=n2; i++) {
if(n % i == 0){
return false;
}
}
return true;
}
Dominic A.
user 12889910
Hamilton, ON
Post #: 10
How about assigning the value of the call to Prime(n) to a variable and testing if that is true?

bool ans = Prime(n);
if (ans == true)
cout << n << endl;

or

if (Prime(n))
cout << n << endl;
Dominic A.
user 12889910
Hamilton, ON
Post #: 11
Also - your prime test will not work.

You have no starting or end points for your loop (n1, n2 are never assigned). Try something like this:

bool Prime(int n) {

for (int c = 2; c < n; ++c) {
if ((n % c ) == 0)
return false;
}
return true;
}
Shivam S.
user 96266692
Toronto, ON
Post #: 3
Tried both of them... i understand my mistake but the program still does'nt work!
It doesnt print the numbers!..heres the code after amends...
(not exactly what u said..but i tried the "if" loop too! that didnt work either! so i stuck to the while loop as it confirms the loop to be infinite! i am a newbie, so i don't knw if i am wrong in it that if loop isnt infinite loop..the computer will stop processing after 1st result comes true...but in while loop it continues to print till range ends! [point me if i am wrong] )




#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

bool prime (int n);

int main() {
int n1;
int n2;
int n;
bool p = prime(n);
cout <<"Enter the starting range and press ENTER:";
cin >> n1;
cout <<"Enter the ending range and press ENTER:";
cin >> n2;

for(n=n1; n<=n2; n++) {
while (p ==true){
cout << n << "" <<endl;

}
}
system("pause");
return 0;
}

bool prime(int n) {
int i;
for (i=2; i<n; ++i) {
if(n % i == 0){
return false;
}
}
return true;
}
Drew W.
user 95133282
Stratford, ON
Post #: 1
For many problems, especially if you're new to programming, it can help to lay out the solution in English, and then add code for each English statement. I think that doing that for this program will help you figure out the problems.

Lets break down what needs to happen in this program:

1. Get the range of numbers to search
2. Iterate over each number in the range, yielding n
3. If n is prime, print n

Now try and map each statement to some piece of code.

1. You're already doing, read n1 and n2 from cin (as an aside, you may want more descriptive variable names such as lower_bound and upper_bound)

2. You're already doing this as well. This is the for-loop

for(int n = n1; n <= n2; ++n)

So what matters here is 3

3. The check to see if n is prime needs to happen inside the loop, not above it. Your loop should look something like

for (int n = n1; n <= n2; ++n) {
if (prime(n)) {
cout << n << endl;
}
}

Then its simply a matter of finding the correct implementation for the prime() function.

Here are some other things to consider:
- What happens if I type "two" instead of "2" for one of the numbers?
- What happens if n1 is larger than n2?
- Do you need to call prime() on every number for a correct answer?
Shivam S.
user 96266692
Toronto, ON
Post #: 4
I had the basic idea ,i.e. the three points you described in english...though i just got into programming 2 weeks ago, so yeah i am new!. You're my savior man! Thanks a lot! Although i cant help asking that why did it work???
(it worked ;) ) I mean what was the problem if i used the "while" loop rather than the "if" loop!? Thanks for the new points u mentioned in the end! ;) i'll tell the pc what to do in those conditions! Thanks again bro!
Dominic A.
user 12889910
Hamilton, ON
Post #: 12
I had the basic idea ,i.e. the three points you described in english...though i just got into programming 2 weeks ago, so yeah i am new!. You're my savior man! Thanks a lot! Although i cant help asking that why did it work???
(it worked ;) ) I mean what was the problem if i used the "while" loop rather than the "if" loop!? Thanks for the new points u mentioned in the end! ;) i'll tell the pc what to do in those conditions! Thanks again bro!
The problem was you were calling prime in main only once (so it was only testing one number). You needed to call it in the loop (as someone pointed out).

If is not a loop like "for" or "while". They can all be block statements, but "if" does not iterate repeatedly.

Try working your way through the code examples in the K&R. That is how I learned to program in C.
Powered by mvnForum

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy