codescracker
c

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:

c program check even or odd

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:

c program odd even

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

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:

c program print series upto n term

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:

print series upto n c program

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker