Java 1 4 1 – Java Program to Print Series 1 4 9 16 25 36 …N

Java 1 4 1: In the previous article, we have discussed about Java Program to Print Series 10 20 30 40 40 50 …N

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

Java Program to Print Series 1 4 9 16 25 36 …N

1, 4, 9, 16: On observing the pattern carefully, we can see that this the numbers in the series are the squares of the number representing there position.

For example:

1 4 9 16 25 36 49 64 81

12              22                32                 42                 52                62               72                82                92

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

Method-1: Java Program to Print Series 1 4 9 16 25 36 …N By Using For Loop

Approach:

  1. Create Scanner class object.
  2. Ask the user to enter a number.
  3. Run a for loop from i=1 to i<=n and print i*i.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) throws Exception 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
    	// take user input for number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        // loop to print the series
        for (int i = 1; i <= n; i++) 
        {
            //finding square and printing series
            System.out.print(i * i + " ");
        }
    }
}

Output:

Enter the number of terms: 8
1 4 9 16 25 36 49 64

Method-2: Java Program to Print Series 1 4 9 16 25 36 …N By Using While Loop

Approach:

  1. Create Scanner class object.
  2. Ask the user to enter a number.
  3. Run a while loop till  i<=n and print i*i.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) throws Exception 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
    	// take user input for number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        // loop to print the series
        int i=1;
        //while loop will continue till i<=n
        while(i<=n) 
        {
            //finding square and printing series
            System.out.print(i * i + " ");
            //increment i
            i++;
        }
    }
}

Output:

Enter the number of terms: 6
1 4 9 16 25 36

Method-3: Java Program to Print Series 1 4 9 16 25 36 …N By Using User Defined Method

Approach:

/n in java: The same for loop or while loop logic can be used just with in a user defined method.

  1. Create Scanner class object.
  2. Prompt the user to enter a number.
  3. Create a user-defined method to print the series.
  4. Inside the method run a while loop till  i<=n and print i*i.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) throws Exception 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
    	// take user input for number of terms
        System.out.print("Enter the number of terms: ");
        int n = sc.nextInt();
        // call the method to print the series
        printSeries(n);
    }

    //user defined method printSeries()
    private static void printSeries(int n) 
    {
        int i=1;
        //while loop will continue till i<=n
        while(i<=n) 
        {
            //finding square and printing series
            System.out.print(i * i + " ");
            //increment i
            i++;
        }
    }
}

Output:

Enter the number of terms: 7
1 4 9 16 25 36 49

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: