Java Program to Print Series 2 4 6 8 10 12 …N

In the previous article, we have discussed about Java Program to Print Series 1 2 4 7 11 …N

In this article we are going to see how to print the series 2 4 6 8 10 12 …N by using Java programming language. Print 2 4 6 8 10 In Java, 2+4+6+8+…+N Formula In Java, 2+4+6+8+…+N Program In Java, 2-4+6-8+10 Series In Java, N in java, Number Series Program In Java, Geometric Sequence Program In Java Using For Loop, For Loop Programs In Java With Output, Java Program To Print Series, 1 4 7 10 Series In Java, 2 4 8 12 Sequence you can learn using java program.

Java Program to Print Series 2 4 6 8 10 12 …N

In this series it can be seen that

  • Numbers at each position ‘i‘, the term is calculated as 2×i OR
  • First element of series is 2 and next elements are calculated as previous element + 2

For example, if the series has 3 terms, the output will be

2×1   2×2   2×3

2        4        6

For example, if the series has 5 terms, the output will be

2×1  2×2  2×3  2×4  2×5

2       4      6       8      10

Let’s see different ways to find the series.

Method-1: Java Program to Print Series 2 4 6 8 10 12 …N By Using Multiplication

Approach:

  • Create Scanner class object.
  • Declare an integer variable ‘n‘ which holds the value of number of terms in the series.
  • Prompt the user to enter the value of variable ‘n
  • Run a for loop from i=0 to i<=n.
  • Inside the loop, print 2*i

Program:

import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)
    {
        //create a Scanner object
        Scanner sc = new Scanner(System.in);
        //prompt the user to enter the number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        //print the series
        System.out.print("The series is: ");
        for (int i = 1; i <= n; i++)
        {
            System.out.print(2*i + " ");
        }
    }
}
Output:

Enter the number of terms: 10
The series is: 2 4 6 8 10 12 14 16 18 20

Method-2: Java Program to Print Series 2 4 6 8 10 12 …N By Using Addition

Approach:

  • Create Scanner class object.
  • Declare an integer variable ‘n‘ which holds the value of number of terms in the series.
  • Declare an integer variable say ‘value‘ and initialize it with value 2.
  • Prompt the user to enter the value of variable ‘n
  • Print ‘value‘ which is having value 2.
  • Run a for loop from i=0 to i<n-1.
  • Inside the loop, print value+2 , as by using addition logic when we will add previous element with 2 then we will get next element of the series.

Program:

import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)
    {
        // create a Scanner object
        Scanner sc = new Scanner(System.in);
        // prompt the user to enter the number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        int value=2;
        // print the series
        System.out.print("The series is: ");
        System.out.print(value+" ");
        for (int i = 0; i < n-1; i++)
        {
            value=value+2;
            System.out.print(value+" ");
        }
    }
}
Output:

Enter the number of terms: 5
The series is: 2 4 6 8 10

Method-3: Java Program to Print Series 2 4 6 8 10 12 …N By Using User Defined Method

Approach:

  • Create Scanner class object.
  • Declare an integer variable ‘n‘ which holds the value of number of terms in the series.
  • Prompt the user to enter the value of variable ‘n
  • Then call a user defined method by passing n as parameter.
  • Inside method run a for loop from i=0 to i<=n.
  • Inside the loop, print 2*i

Program:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //create a Scanner object
        Scanner sc = new Scanner(System.in);
        //prompt the user to enter the number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        //call the method to print the series
        printSeries(n);
    }
    
    //user defined method printSeries() to print series
     private static void printSeries(int n) 
    {
        System.out.println("The series is: ");
        for (int i = 1; i <= n; i++) 
        {
            System.out.print(2*i + " ");
        }
    }
}
Output:

Enter the number of terms: 8
The series is: 
2 4 6 8 10 12 14 16

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Check out yourself:

  • Write A Program To Print 2 4 6 8 10 Using For Loop
  • Write A Program To Print The Following Series 2 4 6,8 10
  • How To Print In Java
  • Print 2 4 6 8 10 In Java

Related Java Programs:

Leave a Comment