Java Program to Print the Series 1 2 10 37 101 226 … N

In the previous article we have discussed about Java Program to print Series 1 2 6 15 31 … N

In this article we are going to see how to print the series 1 2 10 37 101 226 … N by using Java programming language.

Java Program to Print the Series 1 2 10 37 101 226 … N

On observing the pattern carefully, we can see

1st term starts from 1

Then the next term is in the format like previous term element + cube of previous term(position)

For example:

1 2 10 37 101 226 N

                     1+1^3        2+2^3       10+3^3       37+4^3        101+5^3                       +(N-1)^3

First Term(1)        =1              =1

Second Term(2)   =1+1^3     =2

Third Term(3)       =2+2^3     =10

Fourth Term(4)     =10+3^3   =37

Fifth Term(5)         =37+4^3   =101

Like this.

Let’s see different ways to print the series 1 2 10 37 101 226 … N.

Method-1: Java Program to Print the Series 1 2 10 37 101 226 … N By Using For Loop

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the Nth term of the series
  • Prompt the user to enter a number as value of n.
  • Let declare another integer variable say ‘result’ and initialize it to 1
  • Use a for loop from i=1 to i<=n-1 where the loop is incremented by 1
  • Inside the loop we will find the value of 3 times exponent of the Nth term and then add that value with the result.
  • Print the result.

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();
        int result = 1;
        System.out.print(result);
        //for loop to print the series
        for (int i = 1; i <= n-1; i++) 
        {
            result +=Math.pow(i,3); 
            System.out.print(" "+result);
        } 
    }
}
Output:

Enter the Nth term “N” 
5
1 2 10 37 101

Method-2: Java Program to Print the Series 1 2 10 37 101 226 … N By Using While Loop

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the Nth term of the series
  • Prompt the user to enter a number as value of n.
  • Let declare another integer variable say ‘result’ and initialize it to 1
  • Use a while loop from i =1 to i<=n-1 where the loop is incremented by 1
  • Inside the loop we will find the value of 3times exponent of the Nth term and then add that value with the result.
  • 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();
        int result=1;
        System.out.print(result);
        int i=1;
        while(i<=n-1)
        {
            result +=Math.pow(i,3); 
           System.out.print(" "+result);
           i++;
        }      
    }
}
Output:

Enter the Nth term “N” 
6
1 2 10 37 101 226

Method-3: Java Program to Print the Series 1 2 10 37 101 226 … N By Using User Defined Method

Approach:

  • The same logic as method 1 but this time we are moving the for inside a user-defined method
  • Create a Scanner class object.
  • Prompt the user to enter the Nth term “N”
  • Call a method to execute the sum of series.
  • Let declare an integer variable result and initialize it to 1
  • Use a for loop from i =1 to i<=n-1 incremented by 1
  • Inside the loop we will find the value of 3times exponent of the Nth term and then add that value with the result.
  • 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)
    {
        int result = 1;
        System.out.print(result);
        //for loop to print the series
        for (int i = 1; i <=n-1; i++) 
        {
            result +=Math.pow(i,3); 
            System.out.print(" "+result);
        } 
    }
}
Output:

Enter the value of Nth term 'N' 
9
1 2 10 37 101 226 442 785 1297

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Related Java Programs: