In this post, you will learn how to print the cube numbers series using a Java program. Here, we will print a series of cube numbers up to a given number or for a specific number of terms.
Here’s a Java program to print a series of cube numbers up to a given number:
package com.javacodepoint.series;
public class CubeNumbersSeries {
public static void printCubeSeries(int limit) {
System.out.print("Cube Numbers Series: ");
for (int i = 1; i <= limit; i++) {
int cube = i * i * i;
System.out.print(cube + " ");
}
System.out.println();
}
public static void main(String[] args) {
int limit = 5;
printCubeSeries(limit);
}
}
OUTPUT:
Cube Numbers Series: 1 8 27 64 125
Explanation:
In this program, we have a method printCubeSeries
that takes an integer limit
as input and prints the cube 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 cube and then prints it.
See also: Square Numbers Series