   In C programming, what command or code can be used to determine if a number of odd or even?

+1 vote
95 views
In C programming, what command or code can be used to determine if a number of odd or even? posted Dec 2, 2014

if LSB bit is set ,the given no is odd otherwise given no is even.

``````#include <stdio.h>
int num_odd_even(unsigned int num)
{
if(num & 1)
printf("Given no is odd\n");
else
printf("given no is even");
}
`````` answer Dec 2, 2014
+1 vote

There is no command for finding the number is even or odd.
but through command line input you can make your program in a such way that from next time you can use its executable file as a command.

``````#include<stdio.h>
main(int argc,char **argv)
{
int i,num;
if(argc<2)
{
printf("No argument is given\n");
return;
}

for(i=1;i<argc;i++)
{
num = atoi(argv[i]);
if (num%2==0)
printf("%d is even\n",num);
else
printf("%d is odd\n",num);
}
}
`````` answer Dec 2, 2014
+1 vote

printf("%s", (number & 1) ? "ODD" : "EVEN"); answer Dec 4, 2014 by

There is no single command or function in C that can check if a number is odd or even. However, this can be accomplished by dividing that number by 2, then checking the remainder. If the remainder is 0, then that number is even, otherwise, it is odd. You can write it in code as:

``````#include<stdio.h>

int main(){

int number;

printf("Enter any integer: ");
scanf("%d",&number);

if(number % 2 ==0)
printf("%d is even number.",number);
else
printf("%d is odd number.",number);

return 0;

}

Sample output:
Enter any integer: 5
5 is odd number.
`````` answer Dec 2, 2014
Similar Questions