Q:

Java program to find cube 1 to N

belongs to collection: Java Basic Programs

0

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

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to find the Length of Longest Sequenc... >>
<< Java program to check whether given number is Kapr...