Odd and Even numbers in c programming, step by step guide with an easy explanation and detailed source code.

Table of Contents

## Introduction:

Want to know what an odd number is and even a number. It’s simple: it’s a division by 2 or 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 print even numbers is pretty simple. Just initialize the value with even number and increment the value by even number. The same rule can be applied if you want to print all the odd numbers. I won’t explain odd numbers, it’s best you develop some logic for that.

To know more about C Programming and it’s history please read this blog first “**C Programming Complete Guide 2020**“

You can watch the video over here which explains the blog in simple video with VFX animation.

## Example: Print Even Number in C program

This blog explains how to write a program to print even numbers till 20. It’s simple and 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.

main() { int i; }

### Initialization

Now we put value 2 in i. Initialization is a process in C Programming where we initialize variables at the start of program or business logic. Though we have used it to odd and even numbers in C Programming, this is mainly used in while loops in C.

main() { int i; i=2; }

### Business Logic to odd and even number

Business logic is very very easy to odd and 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; } }

Just run the program.

If you want to copy-paste the code please refer our Newtum Github repository link over here.

### Output:

i = 2

i = 4

i = 6

i = 8

i = 10

i = 12

i = 14

i = 16

i = 18

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, or 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.

## Example: Program to check an even or odd number in C using the modulus operator

we will use the if-else condition for finding even or odd numbers. 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.

main() { int i; }

Now, we write printf so that the user will understand to enter any number,

int main() { int i; printf("Enter a number: "); }

then we write scanf statements to accept user input.

int main() { int i; printf("Enter a number: "); scanf("%d",&i); }

### Business Logic to odd and even number

Business 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.

Enter a number: 7

### Output:

Let’s look at the output,

The output says 7 is an odd number.

Enter a number: 7

7 is an odd number

## How to view all topics on C Programming

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

**Amazon Prime link for the USA: ****https://www.amazon.com/dp/B07WFYJW6G**

**Amazon Prime link for the UK: ****https://www.amazon.co.uk/dp/B07WFYQZ1G**

**For Other Countries: **To watch C Programming Content in another country, please visit the URL: **https://newtum.com/course-details/c-programming-online**

**Frequently Asked question about**

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

For check whether a given number is odd or even, we are checking 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, 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.