C program to print all even numbers

Category: C ProgramTags: cprogram loops

Write a C Program to print all even numbers using while loop or for loop with and without if statement. How to print even numbers by using while or for loop. Logic to find all even numbers within the given range of natural numbers in C language.

C program to print all even numbers from 1 to n using if statement

#include <stdio.h>

void main()
{
    int i, n;
    printf("Enter value of n: ");
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        if (i % 2 == 0)
            printf("%d\n", i);
    }
}

C program to print all even numbers from 1 to n without using if statement

#include <stdio.h>

void main()
{
    int i, n;
    printf("Enter value of n: ");
    scanf("%d", &n);
    for (i = 2; i <= n; i=i+2)
        printf("%d\n", i);
}

C program to print all even numbers from 1 to n using if statement and while

#include <stdio.h>

void main()
{
    int i=1, n;
    printf("Enter value of n: ");
    scanf("%d", &n);
    while (i <= n)
    {
        if (i % 2 == 0)
            printf("%d\n", i);

        i++;
    }
}

C program to print all even numbers from 1 to n without using if statement and while

#include <stdio.h>

void main()
{
    int i=2, n;
    printf("Enter value of n: ");
    scanf("%d", &n);
    while (i <= n)
    {
        printf("%d\n", i);
        i = i + 2;
    }
}

Output

Output

Enter value of n: 10
2
4
6
8
10