Java Program to Print the Series 0 7 26 63 …N

In the previous article we have discussed about Java Program to Display Sum of Series X^1 + X^2 + X^3 + ……… X^N

In this article we are going to see how to print the series 0 7 26 63 …N by using Java programming language. 0 7 26 Series In Java,
0, 7, 26, 63 series in java, 0 7 26 63 Series In Java Program, java series program you have to be known already to understand the concept.

Java Program to Print the Series 0 7 26 63 …N

On observing the pattern carefully, we can see

The series is in the form of pow(i,3)-1, where ‘i‘ is the instant nth term from i=1,2,3,…,n

0 7 26 63 124 215 N

(1^3)-1       (2^3)-1       (3^3)-1        (4^3)-1        (5^3)-1        (6^3)-1                             (N^3)-1

Let’s see different ways to print the series 0 7 26 63 …N

Method-1: Java Program to Print the Series 0 7 26 63 …N By Using For Loop

Approach:

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

Enter the Nth term “N” 
5
0 7 26 63 124

Method-2: Java Program to Print the Series 0 7 26 63 …N By Using While Loop

Approach:

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

Enter the Nth term “N” 
7
0 7 26 63 124 215 342

Method-3: Java Program to Print the Series 0 7 26 63 …N By Using User Defined Method

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’, which holds the value of number of terms in the series.
  • Prompt the user to enter a number as a value of ‘n’.
  • Call a user defined method say printSeries() by passing ‘n’ as parameter.
  • Inside method declare a double variable say ‘result’ and initialize it to 1
  • Use a for loop from i=1 to i<=n, where the loop is incremented by 1
  • Inside the loop we will find the value of pow(i,3)-1 up to Nth term and then add the 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 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)-1; 
            System.out.print((int)result+" ");
        } 
    }
}
Output:

Enter the value of Nth term 'N' 
9
0 7 26 63 124 215 342 511 728

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Recommended Reading On: Java Program to Print the Series 3 9 27 81 243 729 …N

Solve these:

  1. 7, 26, 63, 124, 215, 342, ?
  2. 0, 7, 26, ?, 124, 215
  3. 0, 7, 26, 63, 124, ?
  4. 0 7 26 63 What Number Next
  5. 26:63::124:X
  6. 0 7 26 63 Nth Term
  7. 0,7,?,63,124
  8. 7*26

Related Java Programs: