• Register
1 vote


I am new to the programming world. Just started learning with Java. I know my question is the very basic level. But, yeah I do want to know, how can I square a number in Java?

Best regards

15.3k points

Please log in or register to answer this question.

1 Answer

1 vote


Welcome mate.

Let’s come to the point. You can square a number in a couple of ways. The easiest way to square a number is multiplying with itself.  Another way is to call a method named math.pow 

Let’s see how to do it using math.pow

import java.util.*;
public class Square{

  public static void main(String args[]){

  Scanner sc=new Scanner(System.in);
  int number;
  System.out.print("Enter a number: ");
  System.out.println("Square of "+ number + " is: "+ Math.pow(number, 2));



Using a method where we will multiply the number with itself

import java.util.Scanner;
public class SquareNum{

  public static void main(String [] args){

  Scanner sc = new Scanner(System.in);
  System.out.print("Please enter a number: ");
  int num = sc.nextInt();
  System.out.println("Your squared number is: " + square(num));


//Here is our method to get the square

public static int square(int num){

  return num * num;



I hope this will help you to understand better. Keep coding  cool

94.3k points