C Program to print N Even numbers

Description

Get input n and print n even numbers.
Input:
7
Output:
2 4 6 8 10 12 14

Solution

#include<stdio.h>
int main()
{
	int n,i,j;
	printf("Enter a number:");
	scanf("%d",&n);
	printf("First %d even numbers are:\n",n);
	for(j=1,i=0;j<=n;i=i+2,j++)
	{
		printf("%d ",i);
	}
	return 0;
}

Explanation

Instruction(s) inside the for block{} are executed repeatedly till  the second expression (j<=n) is true.

Here i and j are initialized to 0 and 1 respecively. i is incremented by 2 and j is incremented by 1. for each iteration, instructions inside the for block are executed unless j becomes greater than n. so value of i (0 2 4 6 8 10) (n even numbers) in each iteration will be printed using printf statement. 



Interesting Pattern Programs: for more Patterns

Input:
5
Output:
*
**
***
****
*****
****
***
**
*Solution
Input:
5
Output:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *Solution
Input:
5
Output:
*        *
**      **
***    ***
****  ****
**********Solution
Input:
5
Output:
*       *
**     **
***   ***
**** ****
*********Solution
Input:
5
Output:
*       *
**     **
***   ***
**** ****
*********
**** ****
***   ***
**     **
*       *Solution
Input:
5
Output:
*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********Solution