In this post, I am going to write a C program to print ‘even numbers’ from 2 to N. To do that, first I need to get the N value from the user and then iterate using for loop.
To find the ‘even numbers’ I need to divide each value by 2, if the remainder is zero, then it is an ‘even number’. Let’s see the entire C program to print even numbers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> int main() { int i, n; //print a string about the program printf("C Program to print Even numbers!\n"); //get the limit (n) value printf("Enter the limit (n): "); scanf("%d", &n); //start printing even numbers printf("Even Numbers:\n"); //iterate for(i=1; i<=n; i++){ //to find remainder use % modulus operator //if the value of remainder is zero then it is even number if(i % 2 == 0){ printf("%d\n", i); } } } |
Read the comments in the above program to understand it.
Sample output:
1 2 3 4 5 6 7 8 9 10 11 12 |
C Program to print Even numbers! Enter the limit (n): 10 Even Numbers: 2 4 6 8 10 -------------------------------- Process exited after 3.675 seconds with return value 10 Press any key to continue . . . |