Java Program to Series 5 10 15 … N

In the previous article, we have discussed about Java Program to Print Series 4 8 12 16 20 24 …N

In this article we are going to see how to series 5 10 15 … N using Java programming language.

Java Program to Series 5 10 15 … N

In this series the it can be seen that numbers at each position i, the term is calculated as 5 ×i

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

5×1  5×2  5×3

5       10     15

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

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

5      10     15     20     25

Let’s see different ways to find the series.

Method-1: Java Program to Series 5 10 15 … N By Using User Input Value

Approach:

  • Create Scanner class object.
  • Prompt the user to enter a number.
  • Run a for loop i=0 to n.
  • Inside the loop, print 5*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(5*i + " ");
        }
    }

}
Output:

Enter the number of terms: 5
The series is: 5 10 15 20 25

Method-2: Java Program to Series 5 10 15 … N By Using User-Defined Method

Approach:

  • Same approach as method 1 but move steps 3 and 4 inside a user defined method and call it from the main method after taking user input.

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);
    }
     private static void printSeries(int n) 
    {
        System.out.println("The series is: ");
        for (int i = 1; i <= n; i++) 
        {
            System.out.print(5*i + " ");
        }
    }
}
Output:

Enter the number of terms: 5
The series is: 
5 10 15 20 25

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.

Related Java Programs: