Java Program to Print the Series 1 2 9 28 65 …N

In the previous article, we have discussed about Java Program to Print Pell Number Series 0 1 2 5 12 29 70 …N

In this article we are going to see how to print the series 1 2 9 28 65….N  by using Java programming language.

Java Program to Print the Series 1 2 9 28 65 …N

On observing the pattern carefully, we can see the numbers in the series are in the form of a*a*a+1 where value of a starts from 1 and goes up to N.

Example:

N=n*n*n+1

1=0*0*0+1

2=1*1*1+1

9=2*2*2+1

28=3*3*3+1

65=4*4*4+1

Let’s see different ways to print the series 1 2 9 28 65….N.

Method-1: Java Program to Print the Series 1 2 9 28 65 …N By Using For Loop

Approach: 

  • Declare an int variable say ‘n’ which holds the nth value of the series.
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Use a for loop from i=0 to i<n
  • Print the result as i*i*i+1

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
        //Scanner class object created
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the nth value of the series");
        //taking input of 'n' value from user
        int n=s.nextInt(); 
        //printing the series by using for loop
        for(int i = 0; i<n; i++)
        {
          System.out.print(i*i*i+1+ " ");
        }
   }
}
Output:

Enter the nth value of the series
10
1 2 9 28 65 126 217 344 513 730

Method-2: Java Program to Print the Series 1 2 9 28 65 …N By Using While Loop

Approach:

  • Declare an int variable say ‘n’ which holds the nth value of the series
  • Create Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Use a while loop from i=0 to i<n
  • Print the result as i*i*i+1

Program:

import java.util.*;
public class Main
{
   public static void main(String[] args)
   {
        //Scanner class object created
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the nth value of the series");
        //taking input of 'n' value from user
    	int n=s.nextInt(); 
        //printing the series by using while loop
        int i = 0;
        while(i<n)
        {
          System.out.print(i*i*i+1+ " ");
             i++;
        }
   }
}
Output:

Enter the nth value of the series
10
1 2 9 28 65 126 217 344 513 730

Method-3: Java Program to Print the Series 1 2 9 28 65 …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 Scanner class object.
  • Prompt the user to enter a number as value of n.
  • Use a for loop i=0 and i<n
  • Print the result as i*i*i+1

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
   {
        //Scanner class object created
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the nth value of the series");
        //taking input of 'n' value from user
        int n=s.nextInt(); 
        //printing the series by using method calling
        m1(n);
   }
    public static void m1(int n)
    {
        for(int i = 0; i<n; i++)
        {
          System.out.print(i*i*i+1 + " ");
        }
    }
}
Output:

Enter the nth value of the series
10
1 2 9 28 65 126 217 344 513 730

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: