Saturday, 1 October 2011

How to find number is even or odd without using % sign in c language

/* An interview question to find even or odd. */


#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf("Enter the number: ");
scanf("%d",&n);
if(n & 1)
printf("Odd");
else 
printf("Even");
getch();
}


Method 2:


#include<stdio.h>
#include<conio.h>
void main()
{
int n;
float b,c;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
b=n/2.0;
c=b-(int)b;
if(c==0)
printf("\nEven");
else
printf("\nOdd");
getch();
}


Method 3:

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
if((n/2)*2==n)
printf("Even");
else
printf("Odd");
getch();
}


If you want explanation then do comment....

No comments:

Post a Comment