# C Program to Check Even or Odd Number

« Previous Program Next Program »

In this tutorial, we will learn about how to create a program that will ask from the user to enter any number as input and then check whether it is an even number or an odd number. We will also learn about creating a program that will print all the even and odd numbers from start to the number (given by user).

## Check Even or Odd in C

To check whether any given number is an even or an odd number in C programming, you have to ask from the user to enter a number to check for even/odd using divisibility test by 2. That is if the given number is divisible by 2, then it will be an even number, otherwise it will be an odd number.

```#include<stdio.h>
#include<conio.h>
int main()
{
int num;
printf("Enter any number: ");
scanf("%d", &num);
if(num%2 == 0)
printf("\nIt's an even number.");
else
printf("\nIt's an odd number.");
getch();
return 0;
}```

As the above program was written and run under Code::Blocks IDE, therefore after successful build and run, you will get the following output. This is the first snapshot of the sample run:

Now supply any number say 13 and press ENTER key to see the output as shown here in the second snapshot of the sample run:

Here are some of the main steps used in above program:

• Receive any number as input say 13
• Now using if statement, check whether the given number is divisible by 2 without leaving any remainder or not
• If it is without leaving any remainder, then the given number is an even number
• Otherwise it will be an odd number

### Print All Even Numbers upto N

Here is another program that will ask from user to enter the value of N. And then print all the even numbers from starting to N. Let's suppose that if user has supplied 20 as the value of N as input, then all the even numbers will be printed between 1 to 20 including both.

```#include<stdio.h>
#include<conio.h>
int main()
{
int N, i;
printf("Enter the value of N (limit): ");
scanf("%d", &N);
printf("\nAll Even Numbers from 1 to %d:\n", N);
for(i=1; i<=N; i++)
{
if(i%2 == 0)
printf("%d ", i);
}
getch();
return 0;
}```

Here is the final snapshot of the sample run of above program:

### Print All Odd Numbers upto N

This program is similar to the previous program. But this time instead of even numbers, odd numbers gets printed as output.

```#include<stdio.h>
#include<conio.h>
int main()
{
int N, i;
printf("Enter the value of N (limit): ");
scanf("%d", &N);
printf("\nAll Odd Numbers from 1 to %d:\n", N);
for(i=1; i<=N; i++)
{
if(i%2 != 0)
printf("%d ", i);
}
getch();
return 0;
}```

This is the final snapshot of the sample run:

#### Same Program in Other Languages

C Online Test

« Previous Program Next Program »