C Program to Check Number is Even or Odd

78
views
Check Number Even or Odd

In this Program, you’ll learn how to Check Number Even or Odd entered by the User in C Programming.

To Understand this Program to Check Number Even or Odd you should know C Programming and following topics:

  • Arithmetic Operators
  • if…else Statement

A number is even if it is Exactly divided by 2. For Eg: 2, 4, 16, -12, etc.

A number is odd if it is Not Exactly divided by 2. For Eg: 3, 7, 9, -13, etc.

Program to check Entered Number is Even or Odd

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

    printf("Enter a Number: ");
    scanf("%d", &A);

    // True only if the number is perfectly divisible by 2
    if(A % 2 == 0)
        printf("%d is even.", A);
    else
        printf("%d is odd.", A);

    return 0;
}

Output

Enter a Number: -9
-9 is odd.

In the above Program Number entered by the User is Stored in Variable A.

If the number is perfectly divisible by 2 or not is checked using modulus operator.

If the number is perfectly divisible by 2, then the test expression A%2 == 0 evaluates to 1 (true) and the number is even.

However, if the test expression evaluates to 0 (false), the number is odd.

Program to check Entered Number is Even or Odd using Conditional Operator.

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

    printf("Enter a Number: ");
    scanf("%d", &A);

    (A % 2 == 0) ? printf("%d is even.", A) : printf("%d is odd.", A);

    return 0;
}

Output

Enter a Number: 6
6 is even.

Ask your questions about Program to Check Number Even or Odd and clarify your/others doubts. Documentation.