Java Program to Print the Series 1 -2 6 -15 31 …N

In the previous article we have discussed about Java Program to Print the Series 6 11 21 36 56 …N

In this article we are going to see how to print the series 1 -2 6 -15 31 …N by using Java programming language.

Java Program to Print the Series 1 -2 6 -15 31 …N

On observing the pattern carefully, we can see

1st number starts from 1, Then next term is in the form (previous number + square of the previous nth term)

Here the 1st number is positive and the next number is negative sign I.e in odd position it is +ve sign and in even position it is -ve sign.

Then the next number follows a logic

1
-1*(1+1^2)   =-2
2+2^2          =6
-1*(6+3^2)   =-15
15+4^2        =31
-1*(31+5^2) =-56 and so on.
1 -2 6 -15 31 -56 …… N

                       +(1^2)           +(2^2)             +(3^2)             +(4^2)            +(5^2)                                     +((n-1)^2)

Let’s see different ways to print the series 1 -2 6 -15 31 …N

Method-1: print the series 1 -2 6 -15 31 …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 an 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 for loop we will find the value of result=result+Math.pow(i,2).
  • Now we will check the position of nth term in if-else condition If i is at odd position then we will multiply (-1) with the result else multiply (+1) 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);
        //for loop to print the series
        for (int i = 1; i <= n-1; i++) 
        {
            result +=Math.pow(i,2); 
            if(i%2!=0)
                System.out.print(" "+-1*result);
            else
                System.out.print(" "+result);
        } 
    }
}
Output:

Enter the Nth term “N” 
5
1 -2 6 -15 31

Method-2: print the series 1 -2 6 -15 31 …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 integer variable say ‘result’ and initialize it to 1
  • Declare and initialize an integer variable i=1
  • Continue a while loop till i<=n-1, where i is incremented by 1.
  • Inside the while loop we will find the value of result=result+Math.pow(i,2).
  • Now we will check the position of nth term in if-else condition If i is at odd position then we will multiply (-1) with the result else multiply (+1) 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,2); 
            if(i%2!=0)
                System.out.print(" "+-1*result);
            else
                System.out.print(" "+result);
                i++;
        }      
    }
}
Output:

Enter the Nth term “N” 
7
1 -2 6 -15 31 -56 92

Method-3: print the series 1 -2 6 -15 31 …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 user defined method printSeries() by passing n as parameter.
  • Inside the method use a for loop to find the series.
  • Let declare integer variable say ‘result’ and initialize it to 1
  • Use a for loop from i=1 to i<=n-1 incremented by 1
  • Inside the for loop we will find the value of result=result+Math.pow(i,2).
  • Now we will check the position of nth term in if-else condition If i is at odd position then we will multiply (-1) with the result else multiply (+1) 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,2); 
            if(i%2!=0)
                System.out.print(" "+-1*result);
            else
                System.out.print(" "+result);
        } 
    }
}
Output:

Enter the value of Nth term 'N' 
9
1 -2 6 -15 31 -56 92 -141 205

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Answer these:

  • 1, 2, 6, 15, 31, ?
  • How To Write 2^31 In Java
  • 1 2 6 15 31 ?
  • 1 2 6 15 Pattern
  • 1 2 6 15 31 56
  • 1 2 6 15 Sequence

Related Java Programs: