Skip to content

Write a C Program to print all even numbers up to inputted number

by yashan on August 30th, 2011

Write a program in the c programming language to print all the even numbers up to the inputted number by using the for loop statement. Below is the solution of the C program -

/* Write a C Program to print all the even numbers up to the inputted number*/

#include<stdio.h>
void main()
{
     int i,n;

     printf("Enter any number \n");
     scanf(" %d ",&n);

     printf("even numbers up to inputted number are - \n");
     for(i=2;i<=n;i+=2)
     {
             printf(" %d \t",i);
     }
     getch();
     }

Input- Enter any number – 20

Output- Even numbers up to inputted number are  -  2  4  6  8  10  12  14  16  18  20


There is also a second method to make this same program. You can also use ‘ if ‘ statement in the for loop section of the program to print the even numbers. Below is the for loop section of the same C program -

for(i=1;i<=n;i++)
{
        if(i%2=0)
        {
                printf("%d t",i);
        }
}
No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS