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





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.