Check Whether a Number is Even or Odd in C

(Last Updated On: 12/09/2023)

Check Even and Odd Numbers in C programming, a step-by-step guide with an easy explanation and detailed source code. Want to know what an odd number is and even a number? It’s simple: it’s a division by 2 or an increment by 2.

If you want to learn what is initialization and increment, and condition, I won’t be explaining that. For that, please watch our previous episode at https://www.youtube.com/watch?v=iZgldaRRQK8.

Our logic for writing a program to check even numbers is pretty simple. Just initialize the value with an even number and increment the value by an even number. The same rule can be applied if you want to check all the odd numbers. I won’t explain odd numbers; it’s best you develop some logic for that.

To learn more about C Programming and its history, please read this blog first, “C Programming Complete Guide“. You can watch the video over here, which explains the blog in a simple video with VFX animation.

C Program to Check Whether a Number is Even or Odd

We will use the if-else condition for finding even or odd numbers. The modulus (%) operator for finding the given number is even or odd.

Declaring Variables in C Program

Here, to start, we will create a program that will add a number. Here we will create integers using the keyword int variables i. Now, we write printf so that the user will understand to enter any number, then we write scanf statements to accept user input.

Program Logic to check Even or Odd number

Program logic is very, very easy to odd and even numbers. First, We will write an if-else condition; here, we have written i%2 == 0; here, we will use the modulus (%) operator. After this, we will use a printf statement to display the final result.

int main()
{
   int i;
   printf("Enter a number: ");
   scanf("%d",&i);
   if ( i%2 == 0 )
      printf("%d is an even number", i);
   else
      printf("%d is an odd number", i);
}

Let’s run the program. Now here we are entering value 7 as the number.

Output:

Let’s look at the output,

check Even or Odd number

Write a C Program to Check Even Numbers Till 20 Using While Loop

This blog explains how to write a c program to check even numbers till 20. It’s simple, and it all depends on initialization and increment.

Declaring Variables in C Program

Here, to start, we will create a program that will add a number. Here we will create integers using the keyword int variables i. Now we put value 2 in i. Initialization is a process in C Programming where we initialize variables at the start of the program or business logic. Though we have used it for odd and even numbers in C Programming, this is mainly used while loops in C.

Program Logic for even number

Program logic is very, very easy to check, even numbers. First, We will write a while loop, Here we have written i = i + 2, and then we will use a printf statement to display the final result.

main()
{
    int i;
    i=2;
    while( i < 20 )
    {
        printf(" i = %d\n " , i);
        i = i + 2;
    }
}
Output:
c program to check even numbers till 20

Now, look at the increment part. Here we have written i = i + 2. Instead of i = i + 2, we can write i +=2. Run the program. We got our output. We can use 2, 5 or 10 to increment using this operator. All this depends on the program we want to write.

So in this chapter, we had to go through the WHILE loop. To summarize, we can say that while loop is used to do the same type of task again and again. Initialization, condition, and increment are the must-have things for a while loop. Miss anything, and you may end up in an infinite loop.  

The simple way of increment is i = i + 1, but we can use i++ as an increment operator and i – as a decrement operator. We can also use i +=2 operator to give custom increments as per the requirement.

How to view all topics on C Programming

Watch the full programming series on Amazon Prime free with Amazon Prime Membership.

Frequently Asked Questions

How do you find out if a number is even or odd in C?

To check whether a given number is odd or even, we are checking the modulus by dividing a number by 2; if the modulus is 0, then it will be completely divisible by 2; hence number will be even or it will be odd.  

What are the odd numbers?

Odd numbers are whole numbers it cannot be divided exactly into pairs. Odd numbers, when divided by 2, leave a remainder of 1, 3, 5, 7, 9, 11, 13, and 15 … are sequential odd numbers.
Any integer (not a fraction) that cannot be divided exactly by 2. The last digit is 1, 3, 5, 7, or 9 are the odd numbers.

What are the even numbers?

Even numbers are whole numbers it can be divided exactly into pairs. Even numbers, when divided by 2, leave a remainder of 0, 2, 4, 6, 8, 10, 12, 14, 16… are sequential Even numbers. Even numbers have the digits 2, 4, 6, 8, or 0 in their one’s place.

About The Author