Even or Odd
Even numbers are those which are divisible by 2, and which numbers are not divisible 2 is called odd numbers.
But in term of programming for find even number we check remainder ofnumber is zero or not, If remainder is equal to zero that means number is divisible by 2. To find remainder of any number we use modulo (%) operator in C langauge which return remainder as result.
Check give number is Even or Odd
/*************************************************************** Check give number is Even or Odd ***************************************************************/ #include< stdio.h> #include< conio.h> void main() { int no; clrscr(); printf("Enter any number : "); scanf("%d",&no); if(no%2==0) { printf("Even number"); } else { printf("Odd number"); } getch(); }
Output
Enter any number : 5 Odd number
Check give number is Even or Odd Using ternary or conditional Operator
/********************************************************** Check number is Even or Odd ***********************************************************/ #include< stdio.h> #include< conio.h> void main() { int no; clrscr(); printf("Enter any number : "); scanf("%d",&no); (no%2==0) ? printf("Even number") : printf("Odd number"); getch(); }
Output
Enter any number : 6 Even number
Check give number is Even or Odd Using Bitwise Operator
/*************************************************************** Check give number is Even or Odd ***************************************************************/ #include< stdio.h> #include< conio.h> int main() { int num; clrscr(); printf("Enter any number : "); scanf("%d",&num); if(num & 1) { printf("%d is odd",num); } else { printf("%d is even",num); } getch(); }
Output
Enter any number : 20 20 is even
No comments:
Post a Comment