this code consider 0,1 as prime numbers and when i remove "!" sign from 0 and 1 it says an error

#include <iostream>
#include <conio.h>
using namespace std;
int main(){    
int x, y;    
while (true) {        
      
      cout << "Please Enter a positive number\n";        
      cin >> x;        
      if(0 != x || 1 != x) break;        
      cout<<"Please try again with number other then 0 or 1\n";    }     
      for (y = 2; y<x; y++) {        
          if ((x % y) == 0)            
          break;    }     if (y < x)        
          cout << "Not prime number\n";    
          else        
          cout << "prime number\n";
          getch();
          }

Please use some code indenting to make it more readable.

You join the tests for input not being 0 or input not being 1 with an OR (||), which means that if either the input is not 0 OR the input is not 1, it passes the test and is sent on to the prime testing. If the input is one of those values, it obviously cannot be the other. You can fix this test by either testing for equality to the two values , joined by OR and logically inverting that result, or testing for inequality joined by AND.

if( !(0 == x || 1== x) )
//or as
if(0 != x && 1 != x)

When you remove the ! in your code, your statements now read "0 is assigned the value of x" and "1 is assigned the value of x", both of which are errors. You cannot assign a value to a literal. To test for equality, you must use two equal signs, as I've shown above.

(Sidenote: I've seen a few writers suggest that writing conditions based on equality be done in the order shown above, that is if ( 10 == x ) so that when you make the eventual mistake of omitting the second equal sign, the error message will draw your attention to it. If you write if( x = 10 ) , you get no warning that you've made a mistake. Of course, this is no help when comparing variables.)

>Please use some code indenting to make it more readable.
For example:

#include <iostream>
#include <conio.h>

using namespace std;

int main() {    
  int x, y;  

  while ( true ) {        
    cout<<"Please Enter a positive number\n";        
    cin>> x;  

    if ( 0 != x || 1 != x )
      break; 

    cout<<"Please try again with number other then 0 or 1\n";
  }     

  for ( y = 2; y < x; y++ ) {        
    if ( ( x % y ) == 0 )            
      break;
  }

  if ( y < x )        
    cout << "Not prime number\n";    
  else        
    cout << "prime number\n";

  getch();
}

Yes, it's longer. But it's also more consistent and easier to see the flow of execution.

>Of course, this is no help when comparing variables.
Which is precisely why the convention is stupid. If you're taking the extra time to remember that you're comparing to a constant and to reverse the test, you shouldn't have a problem remembering to double check that you're using == instead of =. And of course, most modern compilers will indeed warn you about if ( x = 10 ) if you've set them up properly.

There's no point in hurting readability by changing the expected order of the test (which is what this convention does, despite what advocates of it will tell you) when the benefit is marginal and debatable, at best.

This article has been dead for over six months. Start a new discussion instead.