C program to print even numbers from 1 to N (10, 100, 500, 1000); Through this tutorial, we will learn how to print even numbers from 1 to N (10, 100, 500, 1000) in the c program using for loop, while loop and function.
Algorithm and Programs to Print Even Numbers from 1 to N
Use the following algorithm and program to print even numbers from 1 to n in c:
- Algorithm to Print Even Numbers from 1 to N
- C Program to Print Even Numbers from 1 to N using For Loop
- C Program to Print Even Numbers from 1 to N using While Loop
- C Program to Print Even Numbers from 1 to N using Function
Algorithm to Print Even Numbers from 1 to N
Use the following algorithm to write a program to print even numbers from 1 to N (10, 100, 500, 1000); as follows:
- Step 1: Start Program
- Step 2: Read the number from user and store it in a.
- Step 3: Iterate for or while loop according to a user input a number.
- Step 4: Inside loop, use if with n % 2 == 0 condition to print even number.
- Step 5: Stop Program
C Program to Print Even Numbers from 1 to N using For Loop
/* C Program to Print Even Numbers from 1 to N using For Loop and If */ #include<stdio.h> int main() { int i, number; printf("\n Please Enter the Maximum Limit Value : "); scanf("%d", &number); printf("\n Even Numbers between 1 and %d are : \n", number); for(i = 1; i <= number; i++) { if ( i % 2 == 0 ) { printf(" %d\n", i); } } return 0; }
The output of the above c program; as follows:
Please Enter the Maximum Limit Value : 10 Even Numbers between 1 and 10 are : 2 4 6 8 10
C Program to Print Even Numbers from 1 to N using While Loop
#include<stdio.h> int main() { int i = 2, number; printf("\n Please Enter the Maximum Limit Value : "); scanf("%d", &number); printf("\n Even Numbers between 1 and %d are : \n", number); while(i <= number) { printf(" %d\n", i); i = i+2; } return 0; }
The output of the above c program; as follows:
Please Enter the Maximum Limit Value : 15 Even Numbers between 1 and 15 are : 2 4 6 8 10 12 14
C Program to Print Even Numbers from 1 to N using Function
/* C Program to Print Even Numbers from 1 to N using For Loop and If */ #include<stdio.h> int evenNoList(no){ int i; for(i = 1; i <= no; i++) { if ( i % 2 == 0 ) { printf(" %d\n", i); } } } int main() { int number; printf("\n Please Enter the Maximum Limit Value : "); scanf("%d", &number); printf("\n Even Numbers between 1 and %d are : \n", number); evenNoList(number); return 0; }
The output of the above c program; as follows:
Please Enter the Maximum Limit Value : 20 Even Numbers between 1 and 20 are : 2 4 6 8 10 12 14 16 18 20