027. Write a Program to print first n even numbers in C language

#include<stdio.h>
#include<conio.h> 
main()
{
  int i=2,n;
  clrscr();
  printf("Enter n : ");
  scanf("%d",&n);
  while(i<=n)
  {
	   printf("%d\t",i);
	   i=i+2;
  }
  getch();
}

4 thoughts on “027. Write a Program to print first n even numbers in C language

  1. This code only prints even numbers upto n , not first n even numbers.

    Better approach is :

    firstnEven1(){
    int i,n,t=0;
    printf(“\nEnter n:”);
    scanf(“%d”,&n);
    for(i=1;i<=n;i++){
    t+=2;
    printf("%4d",t);
    }

    }

    firstnEven2(){
    int n,count1=0,i=1;
    printf("\nEnter n:");
    scanf("%d",&n);
    printf("\nFirst %d even numbers:\n",n);
    while(count1 < n){
    if(i%2 != 0){
    printf("%5d",i);
    count1++;
    }
    i++;
    }

    }

Leave a Reply

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