1 8 27 64: In the previous article, we have discussed about Java Program to Print Square Number Series 1 4 9 16 … N
In this article we are going to see how to print the series 1 8 27 64 125….N by using Java programming language. From the beginners point of view 1, 8, 27, 64, pattern, 1 8, 27, 64, sequence, 1, 8, 27, 64 and 125 pattern, series 1, 8, 27, 64, 8*27, 64*125, Foor Loop Java are mentioned in the given below programs of java.
Java Program to Print the Series 1 8 27 64 125….N
On observing the pattern carefully, we can see the numbers in the series are cube of a number. While the series starts from 1 and goes up to N.
Example:
1*1*1 2*2*2 3*3*3 4*4*4 5*5*5 n*n*n
1 | 8 | 27 | 64 | 125 | 216 | 343 | …… | N |
Let’s see different ways to print the series 1 8 27 64 125….N.
Method-1: Java Program to Print the Series 1 8 27 64 125….N By Using For Loop
Approach:
- Declare an int variable say ‘
n
’ which holds the nth value of the series - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Use a for loop from i=1 to i<=n
- Inside for loop, Print the result as
i*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 nth value of the series"); int n=s.nextInt(); // using foor loop to print the series for(int i = 1; i<=n; i++) { System.out.print(i*i*i + " "); } } }
Output: Enter the nth value of the series 5 1 8 27 64 125
Method-2: Java Program to Print the Series 1 8 27 64 125….N By Using While Loop
Approach:
- Declare an int variable say ‘
n
’ which holds the nth value of the series. - Create Scanner class object.
- Prompt the user to enter a number as value of
n
. - Use a for loop from i=1 to i<=n
- Inside loop print the result as
i*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 nth value of the series"); int n=s.nextInt(); // using while loop to print the series int i = 1; while(i<=n) { System.out.print(i*i*i + " "); i++; } } }
Output: Enter the nth value of the series 5 1 8 27 64 125
Method-3: Java Program to Print the Series 1 8 27 64 125….N By Using User Defined Method
Approach:
- Declare an int variable say ‘
n
’ which holds the nth value of the series. - Create Scanner class object.
- Prompt the user to enter a value for ‘n’ as value of
n
. - Call a user defined method by passing ‘n’ value as parameter.
- Inside method use a for loop from i=1 to i<=n
- Print the result as
i*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 nth value of the series"); int n=s.nextInt(); // method call, to print the series m1(n); } public static void m1(int n) { // using for loop to print the series for(int i = 1; i<=n; i++) { System.out.print(i *i* i + " "); } } }
Output: Enter the nth value of the series 5 1 8 27 64 125
Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.
Check out for analysis:
- Print The Following Series Using For Loop:- 1,8,27,64,125,216,……N?
- Print The Following Series Using For Loop:- 1,8,27,64,125,216,……N Input 125 Output :- 1 8 27 64 125?
- Write A Program To Print Following Series 1 8 27 64?
Related Java Programs: