Odd and even in C programming

We will discuss about C programming to check odd and even numbers in this article. We know that even numbers are divisible by two and odd number is not divisible by two. We can implement this idea to determine that a number is odd or even.

Modulus operator (%) is used to determine the reminder. So we can tell that if we get zero as reminder when we divide a number by 2 then the number will be even. Otherwise it will be odd. So, lets implement this idea to make an odd and even C program.

Program to determine odd and even

// c program for odd and even
#include <stdio.h>
int main(){
  int num;

  printf("Enter an integer to check : ");
  scanf("%d", &num);

  if (num % 2 == 0){
    printf("%d is even.\n", num);
  }else{
    printf("%d is odd.\n", num);
    }
  return 0;
}

Output of this odd even program

odd and even program in c

Odd and even program using conditional operator

Conditional operator is a special type of operator and if you don’t know about various operators then it is time to see our another article about operators in C which will let you know all about operators.

// odd and even using condition
#include <stdio.h>
int main(){
  int number;

  printf("Input an integer here : ");
  scanf("%d", &number);

  number % 2 == 0? printf("Even\n"):printf("Odd\n");  // conditional operator

  return 0;
}

Output:

odd and even program in c using conditional operator

Finding odd or even using bitwise operator

If you don’t know about bitwise operator then we will highly recommended you to visit our bitwise operator article in C then try to understand the code bellow.

// odd and even by bitwise operators
#include <stdio.h>
int main(){
  int num;

  printf("Input an integer to check : ");
  scanf("%d", &num);

  if (num & 1 == 1){
    printf("The number is odd.\n");
  }else{
    printf("The number is even.\n");
   }
  return 0;
}

See the output

c odd and even program using bitwise operator

Odd or even program in other way in C

If we divide an even number by 2 and again multiply the result by 2 then we will get the original number.

Suppose,

8 / 2 = 4 and 4 * 2 = 8

Now, 8 ==8

But in case of odd number, when we divide it by 2 will get a fraction number and as we have taken the integer data type, the fraction part will be ignored. Then we will multiply the result by 2 which will give the result not equal to original number.

Suppose,

7 / 2 = 3 and 3 * 2 = 6

Now 7 != 6

#include <stdio.h>
int main(){
  int num;

  printf("Enter an integer to check : ");
  scanf("%d", &num);

  if ((num / 2) * 2 == num){
    printf("Even\n");
  }else{
    printf("Odd\n");
    }
  return 0;
}

See the output of this odd even program

Enter an integer to check : 15
Odd

Process returned 0 (0x0)   execution time : 5.706 s
Press any key to continue.