-9
#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int i = 2;
    while(i<n){
        if(n%i==0){
            cout<<"not Prime";
            break;
        }else{
            cout<<"Prime";
            break;
        }
        i++;
    }
    return 0;
}

This code is written for showing prime or composite/notPrime numbers but 2 is prime & why it is not showing in output?

I write this code for getting for identifying that given number is prime or not. It can work on any number/digit but it can't show about "2" . So why is it?

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39
  • 5
    do a session of [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) and the duck will tell you the issue right away. Then learn real debugging to help you solve far more complex bugs. See [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/995714) – phuclv Nov 05 '22 at 04:51
  • @kotatsuyaki That doesn't even come up, because `while(i < n)` is immediately `false` when `i` and `n` are both `2` so the loop is never entered. – Nathan Pierson Nov 05 '22 at 04:52
  • 1
    You may also want to ask (yourself) why this program thinks 9 is prime. – n. m. could be an AI Nov 05 '22 at 04:55
  • 2
    In general, if you have a `break` in every possible branch of an `if` within your loop, that's a bad sign because it means you aren't actually looping at all. – Nathan Pierson Nov 05 '22 at 05:02
  • If `2` is entered, then the condition `i < n` is `false`, and the loop body is never executed. – Peter Nov 05 '22 at 05:14
  • "It can work on any number/digit" So 2 is an exception and 9 is another exception. How about 15? 21? 25? 49? 81? 333? 5555? Are these primes? If not, why not? – n. m. could be an AI Nov 05 '22 at 07:37
  • I undid your edit which changed the question in a way that an existing answer is practically invalidated. Please do not turn answered questions into moving targets. – Yunnosch Nov 05 '22 at 08:00
  • Somebody please turn the "no output for 2 because no looping" into an answer. Maybe cover the early breaking along. – Yunnosch Nov 05 '22 at 08:02
  • @Yunnosch sorry for that edits but it is also valid question so I edited there, you I am very beginner for this and I don't have any familiar person in my connection to ask this so I asked here. – Shubham Ingole Nov 06 '22 at 08:33

1 Answers1

1

Because 2 is special number it's even prime and you have to add special case for 2.

for more info: https://mathworld.wolfram.com/EvenPrime.html#:~:text=The%20unique%20even%20prime%20number,%22oddest%22%20prime%20of%20all.

Your program will not work for 2 because the condition goes false and it will never enter to the loop!

Update

Look here for primality test

https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test

lablnet
  • 135
  • 2
  • 12
  • yes but what is about 9 then? – Shubham Ingole Nov 05 '22 at 05:34
  • This is an answer to the question (initial version) . However, I am pretty sure that a special case for 2 would not make this a good prime detector. Same problem arises for any other prime, which will all enter the "prime%prime==0" branch and get "not prime", assuming that the early breaking separate issue has been fixed... – Yunnosch Nov 05 '22 at 08:04
  • If you want to check prime number acurately look here https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test @Yunnosch – lablnet Nov 05 '22 at 09:45
  • 1
    I disagree. From that content "Rabin–Miller primality test is a **probabilistic** primality test", which does not match my understanding of "check prime number acurately". – Yunnosch Nov 05 '22 at 09:47
  • What I mean by that with better estimate and instead of adding a lot of base cases – lablnet Nov 05 '22 at 09:48
  • Yes, using lots of special cases would be a bad idea. Thanks for agreeing. (You did not read my comment as recommending to add more special cases, did you?) Though, hmmm, the sieve could be interpreted as making a growing list of ALL previous "special" cases, to be used for finding more ... – Yunnosch Nov 05 '22 at 09:51
  • Actually I also mentioned @ShubhamIngole but stackoverflow do not let me to do so! so you was only mentioned – lablnet Nov 05 '22 at 09:53