Even Number Series Program in Java

Even Number Series Program in Java
Even Number Series Program in Java

import java.util.Scanner;

public class EvenNumberSeries
{
    public static void main(String[] args)
    {
        int n, sum = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of terms=");
        n = sc.nextInt();
        for (int i = 1; i <= n; i++)
        {
            int t = i * 2;
            sum = sum + t;
            if (n == i)
            {
                System.out.println(i * 2);
            }
            else
            {
                System.out.print((i * 2) + "+");
            }
        }
        System.out.println("Sum of even series=" + sum);
    }
}

Output:

Enter number of terms=5
2+4+6+8+10
Sum of even series=30
b. tech. bca icse java java tutorials learn java mca programs
Recent Posts