Printing even numbers from 1 to n is a beginner-level programming task in which we have to print all even numbers in the given range, Let’s write a C program to print even numbers from 1 to n.
Table of Contents
Objective
The objective is clear we have to write a C program that generates and prints even numbers within the specified range of 1 to n.
The logic of printing even numbers from 1 to n is simple forward we will use a loop to iterate from 1 to n. In each iteration, we will check if the number is even or not, if the number is even then we will display it otherwise we will proceed to the other number.
C Program
#include <stdio.h>
void main()
{
int n;
printf("Enter the value of n ");
scanf("%d", &n);
for (int i = 2; i <= n; i++)
{
if (i % 2 == 0)
{
printf("%d ", i);
}
}
}
Output
In this tutorial we use a simple C program that prints all even numbers in the specified range, We can use the same logic in other programming languages like C++, JAVA Python, etc.
2 thoughts on “C Program to Print Even Number From 1 to n”