Table of Square Cube in Java | Java Practice Question – 6

Write an application that calculates the squares and the cubes of the numbers from 0 to 10 and prints the resulting values in table format
https://youtu.be/SNmvvaf7IJk

Begins the declaration of class TableofSquareCube. and remember, the file name for this public class must be TableofSquareCube.java

public class TableofSquareCube  

Note – Don’t forget an opening and closing braces.

Create the main method of java. that is available on every java program

public static void main(String[] args)

Note – Don’t forget an opening and closing braces.

Declaring Variable to Store Integers

int square, cube;

Display Number, Square, and Cube

System.out.print("Number\t Square\t Cube\n");

Use for loop to calculate Square & Cube

for(int i = 0; i <= 10; i++ )
     {
           square = i * i;
           cube = square * i;
           System.out.printf("%d\t\t %d\t\t %d\n",i,square,cube);
       }

Java Question Source Code

public class TableofSquareCube {
    public static void main(String[] args) {
        int square, cube;
        System.out.print("Number\t Square\t Cube\n");
       for(int i = 0; i <= 10; i++ )
       {
           square = i * i;
           cube = square * i;
           System.out.printf("%d\t\t %d\t\t %d\n",i,square,cube);
       }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *