Print sum of first 'n' even numbers - C Program

C program to read a number and set the range. Print all even numbers in the range starting from 1.

OR

Write a C  program to calculate the sum of first n even numbers.


Solution:

#include<stdio.h>
int main()
{
      int i=1,n,s=0;
      printf("Enter number to set range : ");
      scanf("%d",&n);
      printf("\nEven numbers in range 1 to %d :\n\n",n);
      for(i=1;i<=n;i++)
      {
            if(i%2==0)
            {
                  printf("%d  ",i);
                  s=s+i;
            }
      }
      printf("\n\nSum of even numbers in range 1 to %d : %d",n,s);
      return 0;
}


Output:

sum first n even no