C Program to Print all Even Numbers from 1 to N using a while Loop

In this article, we will write a C program to print all even numbers from 1 to n using a while loop.

The program takes the value of N from the user as input and prints all the even numbers from 1 to n on the screen as output.

Sample Input:

Enter the value of n: 10

Output:

All even numbers from 1 to 10 are: 2 4 6 8 10

A number is called an even number if it is divisible by 2 such as 2, 4, 6, 8, etc. But if the number is not divisible by 2, it is called an odd number such as 1, 3, 5, 7, etc.

In C language, we can use the modulus operator(%) to check if the number is even or not. The modulus operator returns the remainder of dividing the first operand by the second operand.

For example, if we divide 5 by 2 using the modulus operator i.e. 5%2 the remainder would be 1, whereas dividing 6 by 2 using the modulus operator i.e. 6%2 gives 0. This means 5 is an odd number whereas 6 is an even number.

The following C program prints all the even numbers from 1 to n using a while loop:

// C program to print all even numbers from 1 to n
#include <stdio.h>

int main(){
	
	int i, n;
	
	printf("Enter the value of n: ");
	scanf("%d", &n);
	
	printf("All even numbers from 1 to %d are: ", n);
	
	i = 1;  // Initialize i with 1
	
	// Run while loop until i reaches n
	while(i<=n){  
		
		// Check if i is even
		if(i % 2 ==0){
			printf("%d ", i);
		}
		
		// Increment i by one on each iteration
		i++;
	}
	
    return 0;
    
}

Output:

Enter the value of n: 10
All even numbers from 1 to 10 are: 2 4 6 8 10

Code Explanation:

  • The program first asks the user to input the value of n and store it in the variable n.
  • As we want to print all even numbers starting from 1, therefore, initialized i=1 before running the while loop.
  • The while loop checks if the current value of i is less than n, if yes, it continues. If not, it terminates.
  • Inside the while loop’s body, the program checks if i is divisible by 2 using the modulus operator(%), if it is, the value of i is printed on the screen.
  • In each iteration of the while loop, the value of i increments by 1 using the increment operator(++).

Example 2: Print Even Numbers without using the If Statement

In the previous example, we used an if statement to check if the number is even or not. We can also print all the even numbers from 1 to n without using the if statement and make the program a bit optimal.

As we know, 2 is the smallest even number, therefore, to print all the even numbers between 1 and n, we can start from 2 and increment it by +2 every time the while loop runs.

The following C program prints all the even numbers from 1 to n without using the if statement:

// C program to print all even numbers from 1 to n
#include <stdio.h>

int main(){
	
	int i, n;
	
	printf("Enter the value of n: ");
	scanf("%d", &n);
	
	printf("All even numbers from 1 to %d are: ", n);
	
	i = 2;  // Since 2 is the smallest even number
	
	// Run while loop until i reaches n
	while(i<=n){  
	
		printf("%d ", i);
		
		// Increment i by 2 to get next even number
		i = i + 2;
		
	}
	
    return 0;
    
}

Output:

Enter the value of n: 10
All even numbers from 1 to 10 are: 2 4 6 8 10

Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts