Java Program to Print the Series 0 4 18 48 100 180 …N

In the previous article we have discussed about Java Program to Print the Series 2 1 1/2 1/4 1/8 … N

In this article we are going to see how to print the series 0 4 18 48 100 180 …N by using Java programming language.

Java Program to Print the Series 0 4 18 48 100 180 …N

On observing the pattern carefully, we can see

The series follows a logic using i.e (i^3)-(i^2) where i starts from 1 and incremented by 1 in each term.

(1^3)-(1^2) = 0

(2^3)-(2^2) = 4

(3^3)-(3^2) = 18

(4^3)-(4^2) = 48 and so on.

Example:

0 4 18 48 100 180 …… N

Let’s see different ways to print the series 0 4 18 48 100 180 …N

Method-1: Java Program to Print the Series 0 4 18 48 100 180 …N By Using For Loop

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the number of terms in the series.
  • Prompt the user to enter a number as value of n.
  • Let declare an double variable say ‘result
  • Use a for loop from i=1 to i<=n where the loop is incremented by 1
  • Inside the for loop we will find the value of result=(i^3)-(i^2).
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the Nth term “N” ");
        int n = s.nextInt();
        double result;
        //for loop to print the series
        for (int i = 1; i <= n; i++) 
        {
            result = Math.pow(i,3)-Math.pow(i,2); 
            System.out.print((int)result+" ");
        } 
    }
}
Output:

Enter the Nth term “N” 
5
0 4 18 48 100

Method-2: Java Program to Print the Series 0 4 18 48 100 180 …N By Using While Loop

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the number of terms in the series.
  • Prompt the user to enter a number as value of n.
  • Let declare double variable say ‘result
  • Declare and initialize an integer variable i=1
  • Continue a while loop till i<=n, where i is incremented by 1.
  • Inside while loop find the value of result = (i^3)-(i^2).
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the Nth term “N” ");
        int n = s.nextInt();
        double result;
        int i=1;
        while(i<=n)
        {
            result = Math.pow(i,3)-Math.pow(i,2); 
            System.out.print((int)result+" ");
            i++;
        }      
    }
}
Output:

Enter the Nth term “N” 
7
0 4 18 48 100 180 294

Method-3: Java Program to Print the Series 0 4 18 48 100 180 …N By Using User Defined Method

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the number of terms in the series.
  • Prompt the user to enter a number as value of n.
  • Call a user defined method say printSeries() by passing n as parameter.
  • Inside method declare an double variable say ‘result
  • Use a for loop from i=1 to i<=n where the loop is incremented by 1
  • Inside the for loop we will find the value of result=(i^3)-(i^2).
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // creating object of scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the value of Nth term 'N' ");
        int n = s.nextInt();
        // calling printSeries method to print the series
        printSeries(n);
    }
    
    //printSeries metthod to print the series
    public static void printSeries(int n)
    {
        double result;
        //for loop to print the series
        for (int i = 1; i <=n; i++) 
        {
            result = Math.pow(i,3)-Math.pow(i,2); 
            System.out.print((int)result+" ");
        } 
    }
}
Output:

Enter the value of Nth term 'N' 
9
0 4 18 48 100 180 294 448 648

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: