Java Program to Print Series 6 12 18 24 28 …N

In the previous article, we have discussed about Java Program to Find the Sum of Series 1 + (x+2)/2! + (2x+3)/3! + (3x+4)/4! + …… N

In this article we are going to see how to print the series 6 12 18 24 28 …N by using Java programming language. Along with that c program to print series 1 4 9 16, C program to print the series 1 3 5 7 9, C program to print series 1, 4, 9, 16.

Read Also: Java Program to Print Series 8 16 24 32 40 …N

Java Program to Print Series 6 12 18 24 28 …N

In this series it can be seen that

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

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

6×1   6×2   6×3

6        12      18

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

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

6       12     18    24     30

Let’s see different ways to find the series.

Method-1: Java Program to Print Series 6 12 18 24 28 …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 6*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(6*i + " ");
        }
    }
}
Output:

Enter the number of terms: 10
The series is: 6 12 18 24 30 36 42 48 54 60

Method-2: Java Program to Print Series 6 12 18 24 28 …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 6.
  • Prompt the user to enter the value of variable ‘n
  • Print ‘value‘ which is having value 6.
  • Run a for loop from i=0 to i<n-1.
  • Inside the loop, print value+6 , as by using addition logic when we will add previous element with 6 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=6;
        // print the series
        System.out.print("The series is: ");
        System.out.print(value+" ");
        for (int i = 0; i < n-1; i++)
        {
            value=value+6;
            System.out.print(value+" ");
        }
    }
}
Output:

Enter the number of terms: 5
The series is: 6 12 18 24 30

Method-3: Java Program to Print Series 6 12 18 24 28 …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 6*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(6*i + " ");
        }
    }
}
Output:

Enter the number of terms: 8
The series is: 
6 12 18 24 30 36 42 48

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.

Try these to build your conceptual skills:

  1. Write a program to print series in java?
  2. Write a program to print the following series 1 4 7 10?
  3. Write a program to print the following pattern example 1 input 8 output 1 4 5 8 2 3 6 7?
  4. Write a program to print series in java?
  5. Write a program to display to the following series based on the user input?
  6. Write a program to print the following series 1 4 9 16?
  7. Write a program to print the following series 2,4,6,8 10?
  8. Java program to print series 1 12 123?
  9. Java program to print series 1 2 4 8 16?
  10. Java program to print series 2 5 10 17?
  11. Java program to print 1 2 4 8 16?
  12. Java program to print 1 12 123?
  13. Java program to print 1 2 4 7 11?

Related Java Programs: