Program in C to find if given number is odd or even


First of all, let’s understand what is odd and even.

#Odd Number

Odd numbers are those which are not divisible by 2 or we cannot split them in half without leaving a fractional number.

If Number%2 != 0
 then Number is odd

% here represents the modulo operator which returns the remainder of the dividend.

For example, we cannot divide 3 by 2 without leaving a reminder.

Odd numbers are

1 3 5 7 9 11 13 15 17 19 21 23 25 27 .....

#Even Number

An even number is just the opposite of an odd number, we can divide even numbers with 2 without leaving any remainder or leaving 0 as the remainder.

If Number%2 == 0
 then Number is even

For example, 4 is completely divisible by 2 so it is an even number.

Even numbers are

2 4 6 8 10 12 14 16 18 and so on...

#Program to find Odd or Even

We already know that % or modulo operator returns the remainder, in the case of our problem, we just need to find the remainder of the given number upon dividing by 2. If the remainder is 0 then the given number is even otherwise it’s odd.

#include <stdio.h>

int main() {
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);

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

    return 0;
}

In the above program, we are getting a number from the user as input and checking its remainder by dividing by 2 and printing out the Number even if it is divisible by 2 otherwise Number is odd.

Thanks for visiting tutorend.com πŸ™‚.