C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Operators & Expressions

Java Program: Square and Cube of a Number (Using *)

import java.util.Scanner;

 

public class SquareCubeCalculator {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

        System.out.print("Enter a number: ");

        int num = sc.nextInt();

 

        int square = num * num;

        int cube = num * num * num;

 

        System.out.println("\nSquare of " + num + " = " + square);

        System.out.println("Cube of " + num + " = " + cube);

 

        sc.close();

    }

}

Output

 
INPUT :
Enter a number: 4

OUTPUT :
Square of 4 = 16
Cube of 4 = 64
 

Explanation

1. Square Calculation

The square of a number is:

number × number

Code:

int square = num * num;

2. Cube Calculation

The cube of a number is:

number × number × number

Code:

int cube = num * num * num;

3. Why Use * Operator?

  • Avoids using built-in methods like Math.pow()
  • Faster and simpler for basic calculations
  • Good for understanding arithmetic fundamentals