prev


Implicit if

24. Even number or not (Logical NOT) (4.7.2009)

/*
Program# 24
date : 25-12-2008
Even number or not
*/

#include <stdio.h>
main(){
int n,x;
printf("\nEnter a integer :");
scanf("%d",&n);
x=n%2;
if(!x) //if(x==0) same as if(!x)
printf("\n%d : Even number\n",n);
else
printf("\n%d : Not an Even number\n",n);
}

output:
Enter a integer : 13
13 : Not an Even number

Things to note:
1.The logical NOT ! operator reverses the result.
2.If a result is a non-zero value then after applying ! on it, the final result will be 0. And if 0 is the result, after applying !, the final result is 1.
3. 0 is considered as false and 1 as true.

next
blog comments powered by Disqus