# Java Program to Print Square Number Series 1 4 9 16 … N

In the previous article, we have discussed about Java Program to Print the Series -1 4 -7 10 -13 16 -19 … N

In this article we are going to see how to print the series 1 4 9 16 25….N by using Java programming language.

## Java Program to Print Square Number Series 1 4 9 16 … N

On observing the pattern carefully, we can see the numbers in the series are squared number.

For example:

 1 4 9 16 25 36 49 …… N

1*1            2*2            3*3            4*4             5*5             6*6             7*7                              n*n

Let’s see different ways to print  the series 1 4 9 16 25….N.

### Method-1: Java Program to Print Square Number Series 1 4 9 16 … N By Using for Loop

Approach:

• Declare an int variable say ‘n’ which holds the nth term of the series.
• Create Scanner class object.
• Prompt the user to enter a number input for ‘n‘.
• Iterate a for loop from i=1 to i<=n
• Inside for loop print the result as i*i

Program:

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

Output:

Enter the number of term ofin the series:
5
1 4 9 16 25

### Method-2: Java Program to Print Square Number Series 1 4 9 16 … N By Using while Loop

Approach:

• Declare an int variable say ‘n’ which holds the nth term of the series.
• Create Scanner class object.
• Prompt the user to enter a number input for ‘n‘.
• Declare and Initialize integer variable i=1
• Iterate a while loop till i<=n
• Inside while loop print the result as i*i

Program:

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

Output:

Enter the number of term of in the series:
8
1 4 9 16 25 36 49 64

### Method-3: Java Program to Print Square Number Series 1 4 9 16 … N By Using while Loop

Approach:

• Declare an int variable say ‘n’ which holds the nth term of the series.
• Create Scanner class object.
• Prompt the user to enter a number input for ‘n‘.
• Declare and Initialize integer variable i=1
• Then call a user defined method printSeries() by passing i and n value as parameter.
• Inside user defined method take a while loop and iterate till i<=n
• And Inside while loop print the result as i*i

Program:

import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Scanner class object created
Scanner s = new Scanner(System.in);
//taking input of 'n' value from user
System.out.println("Enter the number of term of in the series:");
int n=s.nextInt();
//Declared and Initialized an integer variable 'i'
int i = 1;
//calling user defined method printSeries()
printSeries(n, i);
}

//user defined method to print the series
public static void printSeries(int n, int i)
{
//printing the series by using while loop
while(i<=n)
{
System.out.print(i * i + " ");
//incrementing i value by 1
i++;
}
}
}

Output:

Enter the number of term of in the series:
6
1 4 9 16 25 36

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: