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

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);
        }
}

yashan has written 69 articles

5 thoughts on “Write a C Program to print all even numbers up to inputted number

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>