Java program to find the cubes of an n natural numbers

Java program to find the cubes of an n natural numbers. In this tutorial, we’ll construct two Java programs that use the For Loop and While Loop to find the cube of numbers up to a certain integer. Using the for and while loops, the program¬†will find the cude of the first N numbers.

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number ");
int num = sc.nextInt();
for (int i=1;i<=num;i++)
{
System.out.println("Number is "+i+" and cube of "+i+" is "+(i*i*i));
}
}
}

 

Output:

Enter the number 5
Number is 1 and cube of 1 is 1
Number is 2 and cube of 2 is 8
Number is 3 and cube of 3 is 27
Number is 4 and cube of 4 is 64
Number is 5 and cube of 5 is 125