top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

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

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

4 Answers

+2 votes
 
Best answer

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 by Bheemappa G
+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 by Chirag Gangdev
+1 vote

printf("%s", (number & 1) ? "ODD" : "EVEN");

answer Dec 4, 2014 by sivanraj
0 votes

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 by Shivaranjini
Similar Questions
+4 votes

Write the function READ, which is passed two double pointers pointing to the head pointers of two linked lists.

One list will hold even integers, the other one will hold odd integers. READ reads a series of integers. It separates adds odd integers to the first list, and even ones to the second, all in sorted order.

...