-2

I'm getting numbers like 9 and 15 also prime by using this code . pls find the error in the code and if possible Pls edit my code to find the error Here is the code--

`#include<iostream>
    using namespace std;
    int main()
    {
        int n;
        cout<<"Enter the number : ";
        cin>>n;
        int flag=0;
        //to check prime check from 2 to n
        for(int i=2;i<=n;i++)
        {
            if(n%2==0)
            {
                cout<<n<<" is a non-prime number";
                flag++;
                break;
            }
            else
            {
                if(flag==0)
                {
                    cout<<n<<" is a prime number";
                    break;
                }
            }
        }
        return 0;
    }
  • 1
    Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jun 01 '22 at 17:45
  • 2
    Your code is checking only for odd numbers, not prime. As you have correctly identified, 9 and 15 are not prime – smac89 Jun 01 '22 at 17:45
  • so what should i do for prime i also tried using if(n%i==0) that also says 9 to be prime – user19250523 Jun 01 '22 at 17:47
  • There are many algorithms online for finding prime numbers. The easiest will probably be the [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) method. See also [Primality test](https://en.wikipedia.org/wiki/Primality_test) on wikipedia – smac89 Jun 01 '22 at 17:49
  • 3 is prime, 5 is prime, 7 is prime, 9 is... a... statistical anomaly, 11 is prime, 13 is prime. Looks legit to me. – Eljay Jun 01 '22 at 17:49
  • i used if(n%i==0) first so i used that but then too it wasn't giving appropriate results – user19250523 Jun 01 '22 at 17:51
  • Because you are still only checking for odd/even. In either case you print something and `break`. Your `for` loop never runs a second time. – Goswin von Brederlow Jun 01 '22 at 20:30

2 Answers2

4

Your check logic is flawed as you are checking against %2, you should check against %i. I've also added a little optimization as you only need to check up until n/2.

To check if a number is prime:

    #include<iostream>
    using namespace std;
    int main()
    {
        int n;
        cout<<"Enter the number : ";
        cin>>n;
        bool prime=true;
        //to check prime check from 2 to n
        for(int i=2;i<=n/2;i++)
        {
            if(n%i==0)
            {
                prime = false;
                break;
            }
        }
        if (prime)
            cout<<n<<" is a prime number";
        else
            cout<<n<<" is a non-prime number";
        return 0;
    }
  • 3
    Technically you only need to check up to the square root of n ;) – ChrisMM Jun 01 '22 at 17:53
  • Another optimization is as you go up to `sqrt(n)`, use only numbers which are found to be prime because any non-prime will have a prime factor...this is really only useful for very large prime tests – smac89 Jun 01 '22 at 17:56
  • Another option is to perform the 2 check first, then start the loop at 3 and increment by 2. All primes after 2 are odd. – Thomas Matthews Jun 01 '22 at 22:37
0

Your code is not checking if the number is prime or not instead it just check if all the number from 2 to n are even or odd and as the value of i become an odd number (which is 3, just after its first iteration) the code executes these lines

cout<<n<<" is a prime number";
flag++;

Also what is the point of

flag = 0 

if you are using break for terminating the loop.

You must be trying to write something like this

#include<iostream>
using namespace std;
int main()
{
    int n;
    cout<<"Enter the number : ";
    cin>>n;
    int flag=0;
    for(int i=2;i<=n;i++)
    {
        if(n%i==0)
        {
            cout<<n<<" is a non-prime number";
            flag++;
            break;
        }
    }
    if(flag==0)
    {
        cout<<n<<" is a prime number";
        break;
    }
    return 0;
}

Also to make this code better write

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

instead

Mayank_pawar
  • 54
  • 10