C program to find odd & even Numbers using different methods

Programming languages or concepts
0

 

C program to find Even or odd number using different methods


  In this tutorial we learn how to find odd and even numbers by using different methods,

C program to check odd or even number simple program:

#include <stdio.h>

int main()
{
  int n;

  printf("Enter an integer\n");
  scanf("%d", &n);
 
  if ((n/2)*2 == n)
    printf("Even\n");
  else
    printf("Odd\n");

  return 0;
}


Odd or even program in C using modulus operator:


 in decimal number system, even numbers are exactly divisible by two and odd number not.
We can use modulus operator '%' which returns the remainder, for example, 4%4 = 0 (remainder when four is divided by four).


Odd or even program in C using modulus operator



#include <stdio.h>

int main()
{
  int n;

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

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

  return 0;
}


We can also used bitwise operator AND   ( & ) to check odd and even numbers.
consider binary of 5 (0101), (7 & 1 = 1). You may observe that the least significant bit of every odd number is 1. Therefore (odd_number & 1) is one always and also (even_number & 1) is always zero.

C program to find odd or even using bitwise operator


#include <stdio.h>

int main()
{
  int n;
   
  printf("Input an integer\n");
  scanf("%d", &n);

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


C program to check even or odd using conditional operator

#include <stdio.h>

int main()
{
  int n;

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

  n%2 == 0 ? printf("Even\n") : printf("Odd\n");

  return 0;
}



Recommended for you



Post a Comment

0Comments

Post a Comment (0)
close