Saturday, 28 April 2012

odd or even



#include<stdio.h>

void main()
{
   int n;

   printf("Enter an integer\n");
   scanf("%d",&n);

   if ( n%2 == 0 )
      printf("Even\n");
   else
      printf("Odd\n");

   getch();
}


C program to check odd or even using bitwise operator:-


#include<stdio.h>
#include<conio.h>

void main()
{
   int n;

   printf("Enter an integer\n");
   scanf("%d",&n);

   if ( n & 1 == 1 )
      printf("Odd\n");
   else
      printf("Even\n");

   getch();
}




C program to check odd or even without using bitwise or modulus operator:-

#include<stdio.h>
#include<conio.h>

void main()
{
   int n;

   printf("Enter an integer\n");
   scanf("%d",&n);

   if ( (n/2)*2 == n )
      printf("Even\n");
   else
      printf("Odd\n");

   getch();
}

No comments:

Post a Comment