In this post, you will learn how to print the square numbers series using a Java program. Here, we will print a series of square numbers up to a given number or for a specific number of terms.
Here’s a Java program to print a series of square numbers up to a given number:
package com.javacodepoint.series;
public class SquareNumbersSeries {
public static void printSquareSeries(int limit) {
System.out.print("Square Numbers Series: ");
for (int i = 1; i <= limit; i++) {
int square = i * i;
System.out.print(square + " ");
}
System.out.println();
}
public static void main(String[] args) {
int limit = 10;
printSquareSeries(limit);
}
}
OUTPUT:
Square Numbers Series: 1 4 9 16 25 36 49 64 81 100
Explanation:
In this program, we have a method printSquareSeries
that takes an integer limit
as input and prints the square numbers from 1 up to the given limit
. The method uses a for loop to iterate through the numbers from 1 to the limit
, and for each number, it calculates its square and then prints it.
See also: Even Numbers Series: 2,4,6,8,10