In this tutorial learn how you can write a program to calculate the square of a number in java. We use the scanner class to get input from the user.

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.

Write a Program to Calculate the Square of a Number in Java

Here is the code to calculate the square of a number in Java programming.

//Write a Java Program to Calculate the Square of a Number.
import java.util.Scanner;

public class Square {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int number, square;
        System.out.println("Enter the number that you want to square: ");
        number = input.nextInt();
        square = number * number;
        System.out.println("The square of " + number + " is " + square + ".");
        input.close();
    }
}

Test Live At Jdoodle

Conclusion

This is how you can write a program to calculate the square of a number in java programming. Comment below if you like our tutorial.