Even or Odd Numbers In C

Write a Program To check given Number is Odd Or Even?

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;
}


C program to find odd or even using bitwise operator


#include <stdio.h>
#include<conio.h>
void main()
{
  int n;
 
  printf("Input an integer\n");
  scanf("%d", &n);
  if (n & 1 == 1)
    printf("Odd\n");
  else
    printf("Even\n");
 
  getch();
}

C program to check odd or even without using bitwise or modulus operator


#include<stdio.h>
#include<conio.h>
void main()
{
  int n;
  printf("Enter an integer\n");
  scanf("%d", &n);

  if ((n/2)*2 == n)
    printf("Even\n");
  else
    printf("Odd\n");
  getch();
}
In C programming language, when we divide two integers, we get an integer result, for example, 7/3 = 2. So we can use it to find whether a number is odd or even. Even numbers are of the form 2*n, and odd numbers are of the form (2*n+1) where n is is an integer. We can divide an integer by two and then multiply it by two if the result is the same as the original number then, the number is even otherwise odd. For example, 11/2 = 5, 5*2 = 10 (which isn't equal to eleven), now consider 12/2 = 6 and 6*2 = 12 (same as the original number). This logic can be used to determine if a number is odd or even.

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;
}

No comments:

Post a Comment