Jump to content

JAVA show the cube and squares of numbers 1-10 using a loop

Sergio45
Go to solution Solved by Mr_KoKa,

I think there shouldn't be ; at the end of the line with for, and your loop condition number < 10 makes it from 1 to 9. Loop will execute only when number is less than 10 so 9 is ok but 10 is not less than ten, number <= 10 will do.

So I can figure out how to start a loop and have it go through numbers 1 and 10. While the loop is running it needs to show the cube and square of the number. Any help will be appreciated.

Thanks!

public class CubesAndSquares
{
	public static void main (String [] args)
	{
		int number;
		int square;
		int cube;
		
		
		System.out.println("Number\tSquare\tCube");
		
		
		
		for (number=1; number<10; number++);
		{
			square= number * number;//
			cube= number * square;
			System.out.println(number+"\t"+square+"\t"+cube);
			
		}
	}

}

 

Link to comment
Share on other sites

Link to post
Share on other sites

I think there shouldn't be ; at the end of the line with for, and your loop condition number < 10 makes it from 1 to 9. Loop will execute only when number is less than 10 so 9 is ok but 10 is not less than ten, number <= 10 will do.

Link to comment
Share on other sites

Link to post
Share on other sites

Mr_KoKa,

 

Thank you for the advise. I am currently laughing my butt off from not seeing the ; at the end of the for statement. I did change the 10 to 11 to make the loop go to 10. 

 

public class CubesAndSquares
{
	public static void main (String [] args)
	{
		int number;
		int square;
		int cube;
		
		
		System.out.println("Number\tSquare\tCube");
		
		
		
		for (number=1; number<11; number++)
		{
			square= number * number;//
			cube= number * square;
			System.out.println(number+"\t"+square+"\t"+cube);
			
		}
	}

}

 

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Sergio45 said:

Mr_KoKa,

 

Thank you for the advise. I am currently laughing my butt off from not seeing the ; at the end of the for statement. I did change the 10 to 11 to make the loop go to 10. 

 


public class CubesAndSquares
{
	public static void main (String [] args)
	{
		int number;
		int square;
		int cube;
		
		
		System.out.println("Number\tSquare\tCube");
		
		
		
		for (number=1; number<11; number++)
		{
			square= number * number;//
			cube= number * square;
			System.out.println(number+"\t"+square+"\t"+cube);
			
		}
	}

}

 

Instead of making it 11, just do <=. Obviously both will work, but <=10 is just more straight forward to the reader. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

does java not have powers ? In python you can do

number ** 2 to square

number ** 3 to cube

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

  • 5 years later...
On 8/25/2016 at 6:33 AM, Sergio45 said:

So I can figure out how to start a loop and have it go through numbers 1 and 10. While the loop is running it needs to show the cube and square of the number. Any help will be appreciated.

Thanks!


public class CubesAndSquares
{
	public static void main (String [] args)
	{
		int number;
		int square;
		int cube;
		
		
		System.out.println("Number\tSquare\tCube");
		
		
		
		for (number=1; number<10; number++);
		{
			square= number * number;//
			cube= number * square;
			System.out.println(number+"\t"+square+"\t"+cube);
			
		}
	}

}

 

I think you can use this in java shell to find cube of numbers 

              

    void Cuberoot(int n){

      for ( i = 1 ; i<=10 ; i++) {               //use for loop to get square root from 1 to 10 

       System.out.printf("%d  *  %d  = %d " , n , i ,  i * i ).println()

}

         //now to get cube root just update and  add another i in that

 

System.out.printf("%d  *  %d  = %d " , n , i ,  i * i * i ).println()  //use this to get cube root

 

// a class of cuberoot form  like this enter value to get cube or square root

  

   Cuberoot(5)

    

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×