Java Program to Print the Series 1 9 17 33 49 73 97 … N

In the previous article we have discussed about Java Program to Print the Sum of Series 3 + 7+ 13 + 21 + … + N

In this article we are going to see how to print the series 1 9 17 33 49 73 97 … N by using Java programming language.

Java Program to Print the Series 1 9 17 33 49 73 97 … N

On observing the pattern carefully, we can see the series follows a logic

If i is odd then 2*(i*i)-1

If i is even then 2*(i*i)+1

Example:

2*(i*i)-1 = 2*(1*1)-1 = 1

2*(i*i)-1 = 2*(2*2)+1 = 9

2*(i*i)-1 = 2*(3*3)-1 = 17

2*(i*i)-1 = 2*(4*4)+1 = 33 … and so on.

1 9 17 33 49 73 …… N

Let’s see different ways to

Method-1: Java Program to Print the Series 1 9 17 33 49 73 97 … 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 integer 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 according to the position of i. If i is odd then result=2*(i*i)-1, If i is even then result=2*(i*i)+1
  • 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 number of terms  ");
        int n = s.nextInt();
        int result;
        //for loop to print the series
        for (int i = 1; i <= n; i++) 
        {
            if(i%2==0)
            {
                result=(int) (2*Math.pow(i,2)+1);
                System.out.print(result+" ");
            }
            else
            {
                result=(int) (2*Math.pow(i,2)-1);
                System.out.printf(result+" ");
            }
        } 
    }
}
Output:

Enter the number of terms 
5
1 9 17 33 49

Method-2: Java Program to Print the Series 1 9 17 33 49 73 97 … 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 integer 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 we will find the value of result according to the position of i. If i is odd then result=2*(i*i)-1, If i is even then result=2*(i*i)+1
  • 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;
        int i=1;
        while(i<=n)
        {
            if(i%2==0)
            {
                result=(int) (2*Math.pow(i,2)+1);
                System.out.print(result+" ");
            }
            else
            {
                result=(int) (2*Math.pow(i,2)-1);
                System.out.printf(result+" ");
            }
            i++;
        } 
    }
}
Output:

Enter the number of terms 
7
1 9 17 33 49 73 97

Method-3: Java Program to Print the Series 1 9 17 33 49 73 97 … N By Using User Defined Method

Approach:

  • Create a Scanner class object.
  • Prompt the user to enter a value of n which holds the number of terms in the series.
  • Call a user defined method printSeries() by passing n as parameter.
  • Inside the method declare integer variable say ‘result’.
  • Inside the for loop we will find the value of result according to the position of i. If i is odd then result=2*(i*i)-1, If i is even then result=2*(i*i)+1
  • 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 ;
        for(int i = 1; i<=n; i++)
        {
            if(i%2==0)
            {
                result=(int) (2*Math.pow(i,2)+1);
                System.out.print(result+" ");
            }
            else
            {
                result=(int) (2*Math.pow(i,2)-1);
                System.out.printf(result+" ");
            }
        }
    }
}
Output:

Enter the number of terms 
9
1 9 17 33 49 73 97 129 161

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Programs: