# Java Program to Print the Series 1 9 25 49 81 121 …N

In the previous article we have discussed about Java Program to Display the Series 1.5 3.0 4.5 6.0 7.5 …N

In this article we are going to see how to print the series 1 9 25 49 81 121 …N by using Java programming language

## Java Program to Print the Series 1 9 25 49 81 121 …N

On observing the pattern carefully, we can see 1st number starts from 1 and the next number is i*i where i starts with 1 and increments with i+2, Where N is the number of terms in the series.

Example:

Suppose the value of N=5
i=1, 1*1 = 1
i=3, 3*3 = 9
i=5, 5*5 = 25
i=7, 7*7 = 49
i=9, 9*9 = 81
Then the series is 1 9 25 49 81

Let’s see different ways to print the series 1 9 25 49 81 121 …N.

### Method-1: Java Program to Print the Series 1 9 25 49 81 121 …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 value of n.
• Let declare an integer variable result
• Use a for loop from i=1 to i<=2n-1 and i incremented by i+2
• Inside loop we will find the value of result= i*i
• 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;
//for loop to print the series
for (int i = 1; i <= 2*n-1; i+=2)
{
result = i*i;
System.out.print(result+" ");
}
}
}

Output:

Enter the Nth term “N”
5
1 9 25 49 81

### Method-2: Java Program to Print the Series 1 9 25 49 81 121 …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 value of n.
• Let declare an integer variable result
• Declare and initialize an integer variable i=1
• Continue a while loop till i<=2n-1 and i incremented by i+2
• Inside loop we will find the value of result= i*i
• 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<=2*n-1)
{
result =i*i;
System.out.print(result+" ");
i+=2;
}
}
}
Output:

Enter the Nth term “N”
6
1 9 25 49 81 121

### Method-3: Java Program to Print the Series 1 9 25 49 81 121 …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 value of n.
• Then call a user defined method printSeries() by passing n as parameter.
• Inside method declare an integer variable result
• Use a for loop from i=1 to i<=2n-1 and i incremented by i+2
• Inside loop we will find the value of result= i*i
• 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);
}
//user defined method printSeries() to print the series
public static void printSeries(int n)
{
int result;
//for loop to print the series
for (int i = 1; i <=2*n-1; i+=2)
{
result =i*i;
System.out.print(result+" ");
}
}
}

Output:

Enter the value of Nth term 'N'
6
1 9 25 49 81 121

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples.

Related Java Programs: