Prime Number

How to find the Prime number by using If and Else only?

 ``12345678910111213141516171819202122232425262728293031`` `````` #include using namespace std; int main () { int n; cout << "Enter an intger !"<>n; if(n == 2) cout<<"2 is the only even prime number"<

Can you help me with it please, still not sure whether its right or not!
It's not correct, one is not a prime number, yet your program will say that it is. Also I don't know where line 20 came from, anything evenly divisible by a number beside itself and 1 is NOT a prime so 15 would be called prime by this program. You may need to do a little more research on prime numbers before tackling this.
Last edited on
What are your limitations? What do you mean "using If and Else only"?

If you literally mean that you can use only if and else, then there is no way you can compare numbers, so there is no way to achieve your goal.

If you can use other things as well, I suggest using <cmath> to get sqrt() function, but first - do research on prime numbers.

Prime number is not the number that is divisible by 3 or 5 or 7(yes, you made mistake there).
Prime number is the number that is divisible by 1 and itself. How to check for prime number? Think, or look for answer in web.
You could do it, with enough if/else statements. For example:
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include int main() { int n = 0; bool isprime = 1; //We assume a number is prime until proven otherwise. std::cin >> n; if(n < 2) { isprime = 0; } if(n % 2 == 0 && n > 2) { isprime = 0; } if(n % 3 == 0 && n > 3) { isprime = 0; } if(n % 5 == 0 && n > 5) { isprime = 0; } if(n % 7 == 0 && n > 7) { isprime = 0; } if(n % 11 == 0 && n > 11) { isprime = 0; } //You have to write out an if/else statement for all the primes less than your number! std::cout << n << " is prime is " << isprime << "\n"; //1 = Yes it's prime or 0 = No it's not prime } ``````

It's obviously much better to use a loop here, but it is possible using only if/else, but not practical or useful in anyway.
Last edited on
Well, it would only be possible for a limited range of values for `n`.
A general solution using that techique would require an unlimited (effectively infinite) number of lines of code.
Er, it would be better to assume the number is not prime than that it is.

In any case, here's my entry for the random restriction.

 ``12345678910111213141516171819202122`` ``````#include #include #include #include using namespace std; int main() { size_t n; cout << "Enter number to test for primality> "; cin >> n; ifstream primes( "primes.txt" ); typedef istream_iterator iter; if (find( iter( primes ), iter(), n ) != iter()) cout << "it's prime!\n"; else cout << "it's composite.\n"; return 0; }``````
I don't think it matters if you choose not prime or is prime anyway? If the program works, the answer will be correct, regardless of what was chosen.
No. Numbers need to be proven prime. I can go on about how special primes are and stuff, but it doesn't really matter.

But it comes down to a simple truth-table problem -- the one that few people seem to grasp:

 ``` p := function returns that n is prime q := n really is prime r := answers the question: "Is n prime?" +---+---+-------+ | p | q | p-->q | Does (function result) imply (n is prime)? +---+---+-------+ | T | T | T | A prime number is prime. | F | F | T | A not prime number is not prime. +---+---+-------+ | F | T | T | case 1 | T | F | F | case 2 +---+---+-------+ ```

The first two items are obvious. A prime number is prime, a not prime number is not. However, a single, simplistic primality test cannot possibly guarantee either of these truth implications.

So the trick is now which of the remaining truth implications we want our function to have.

In case 1, we have asked the question: "Is this (truly prime) number prime?" and received the response "no". I may not be able to trust the function when it says "no", but I can still trust the function when it says "yes". (That is the meaning of the implication.)

In case 2, we have asked the question: "Is this (not prime) number prime?" and received the (useless) response "yes". I cannot trust the function.

Get it? If the question is, "is this number prime?" I must be able to trust that the response is valid for all truly prime numbers.

Hope this helps.

It is actually possible to prove a number prime using just two or three simple functions, though...
Last edited on
Thank for the feed backs folks

As it's mentioned in the question is only if and else!

I tired out again and want to check whether it's fine this way!

 ``12345678910111213141516171819202122232425`` ``````#include using namespace std; int main () { int n; cout<<"inter an integer!"<>n; if(n==2 || n==3 || n==5 || n==7) cout<<"n is prime"<

Thanks!
I stuck a while loop in there to check how far it is correct. It works just fine for n <= 120.
Well, it gives an incorrect result for n=1 and no output whatsoever for n=4.
Topic archived. No new replies allowed.