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

6 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++;
    }

    }

    1. Hi Pravalika, getch(), accepts users input from the keyboard, a character. Its commonly used to view the until the user enters something and then the program can exit. But its real purpose is to accept and return the character entered by the user. In some compilers, a string can be entered but the only first letter will be returned only.

Leave a Reply

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