C Program to Find the Sum of first n even Numbers

In this article, we are going to write a c program to find the sum of first n even numbers

We will make this program in the following way -:

• C Program to Find the Sum of first n even Numbers Using For Loop
• C Program to Find the Sum of first n even Numbers Using While Loop
• C Program to Find the Sum of first n even Numbers Using Do While Loop
• C Program to Find the Sum of first n even Numbers Using Function

Let’s understand all these methods one by one

C Program to Find the Sum of first n even Numbers Using For Loop

To make this program, you must first know about the loop because we are going to use the concept of loop to make this program.

There are mainly three types of loops –: While Loop, Do While Loop, and For Loop.

Here we are going to make this program with the help of these three types of loops.

Let’s first make this program with the help of simple loop (For Loop)

Algorithm

• Program Start
• Variable Declaration (n,i,sum)
• Input Numbers
• Calculating Sum
• Displaying the Sum of first n even Numbers
• Program End

Program

``````//C Program to Find the Sum of first n even Numbers Using For Loop

#include<stdio.h>
void main()
{
//Declaring Variable
int n, i, sum = 0 ;

//Input Number
printf("Enter a Number\n");
scanf("%d",&n);

for(i=1;i<=n;i++)
{   if(i%2==0)
{ sum = sum + i;
}
}
printf("\nSum of first %d even Numbers: %d",n,sum);

}``````

Output

``````Enter a Number :
10

Sum of first 10 even Numbers : 30``````

C Program to Find the Sum of first n even Numbers Using While Loop

Program

``````//C Program to Find the Sum of first n even Numbers Using While Loop

#include<stdio.h>
void main()
{
//Declaring Variable
int n, i=1, sum = 0 ;

//Input Number
printf("Enter a Number\n");
scanf("%d",&n);

while(i<=n)
{   if(i%2==0)
{
sum = sum + i;
}
i++;
}

printf("\nSum of first %d even Numbers: %d",n,sum);

}``````

Output

``````Enter a Number :
5

Sum of first 5 even Numbers : 6``````

C Program to Find the Sum of first n even Numbers Using Do While Loop

Program

``````//C Program to Find the Sum of first n even Numbers Using Do While Loop

#include<stdio.h>
void main()
{
//Declaring Variable
int n, i=1, sum = 0 ;

//Input Number
printf("Enter a Number\n");
scanf("%d",&n);

do
{
if(i%2==0)
{
sum = sum + i;
}
i++;
}while(i<=n);

printf("\nSum of first %d even Numbers: %d",n,sum);

}``````

Output

``````Enter a Number
20
Sum of first 20 even Numbers : 110``````

C Program to Find the Sum of first n even Numbers Using Function

To Calculate the sum of first n even numbers through a function, first you need to know about the function. You can read about the function from the link given below.

Algorithm

• Program Start
• Declaring variables
• Input Number
• Calling Function to calculate Sum of first n even Numbers
• Print the Sum of first n even Numbers
• Program End

Program

``````//C Program to Find the Sum of first n even Numbers Using Function

#include<stdio.h>
int sum(int n);
void main()
{
//Declaring Variable
int n, s;

//Input Number
printf("Enter a Number\n");
scanf("%d",&n);
s = sum(n);
printf("\nSum of first %d even Numbers: %d",n,s);

}
int sum(int n)
{ int i, sum =0;
for(i=1;i<=n;i++)
{
if(i%2==0)
{
sum = sum + i;
}
}
return sum;

}``````

Output

``````Enter a Number
30

Sum of first 30 even Numbers : 240``````

Conclusion

So friends, in this article, we have written a program to calculate the sum of first n even numbers in four different ways.

• C Program to Find the Sum of first n even Numbers in a Given Range (Using For Loop)
• C Program to Find the Sum of first n even Numbers Using While Loop
• C Program to Find the Sum of first n even Numbers Using Do While Loop
• C Program to Find the Sum of first n even Numbers Using Function

If you have difficulty to understanding any program or have any doubts or questions, then tell me in the comment below.

Jeetu Sahu is A Web Developer | Computer Engineer | Passionate about Coding, Competitive Programming, and Blogging