Solution:
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: ");
num=sc.nextInt();
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 