Write a program in Java to calculate the sum of first n even numbers. ## Sum of First n Even Numbers using For Loop

To find the sum of first n even numbers we will check each natural number starting from 1 whether they are even or not. If yes then we add that number to the `sum` and increment the value of `count`, else we check for the next number.

We will use `count` as the loop variable because if `count` value reaches `n` then it mean we have successfully added n even numbers.

See the java source code.

output

## Sum of First n Even Numbers using while Loop

The logic of the program is the same as the above program only we have replaced for loop with while loop. While loop should run until count is less than n.

output

## Sum of Even Numbers using Formula

Formula n*(n+1) gives the sum of first n even numbers. Let’s use it in our java program to output the sum of the first 100 even numbers.

output

Note: The above programs computes the sum of first n even numbers. They don’t calculate the sum of even numbers from 1 to n.

Comment below if you have any doubts or suggestions.