Dremendo Tag Line

Print the sum of 1st 10 even numbers using for loop in Java

for Loop - Question 1

In this question, we will see how to print the sum of 1st 10 even numbers in Java programming using for loop. To know more about for loop click on the for loop lesson.

Q1) Write a program in Java to print the sum of 1st 10 even numbers using for loop.

Program

public class Q1
{
    public static void main(String args[])
    {
        int i,n=2,s=0;

        for(i=1; i<=10; i=i+1)
        {
            System.out.println(n);
            s=s+n;
            n=n+2;
        }
        System.out.println("Sum of 1st 10 even numbers = " + s);
    }
}

Output

2
4
6
8
10
12
14
16
18
20
Sum of 1st 10 even numbers = 110
video-poster