Home »
Java Programs »
Java Basic Programs
Java program to find cube 1 to N
In this java program we are going to find the cube from 1 to N, where N will we the user input.
Submitted by Chandra Shekhar, on January 05, 2018
Given N (Maximum limit), we have to find cube from 1 to N using java program.
Example:
Input: 5
Output: 1, 8, 27, 64, 125
Program to find cube from 1 to N in java
import java.util.Scanner;
public class DisplayCubeUptoN
{
public static void main(String[] args)
{
// declare here
int n,i;
// enter number upto which you have to find cube.
System.out.print("Enter the last number for cube : ");
Scanner Sc= new Scanner(System.in);
// scan the number and store it variable.
n = Sc.nextInt();
// loop to find cube for all possile numbers.
for(i=1;i<=n;i++)
{
System.out.println("Cube of " +i+" is : "+(i*i*i));
}
}
}
Output
Enter the last number for cube : 5
Cube of 1 is : 1
Cube of 2 is : 8
Cube of 3 is : 27
Cube of 4 is : 64
Cube of 5 is : 125
Java Basic Programs »