To square a number in java there are perform following step :-
- Create Scanner class Object and pass System.in method to take input value.
- sc.nextInt method read only int type value.
- Declare one argument type test method with parameter n.
- square are the numbers generated when a value is multiplied by itself.
- Here i=n*n means 9=3*3 simply square a number itself.
import java.util.*;
public class SquareNumber {
int test(int n) {
int i = n * n;
return i;
}
public static void main(String[] args) {
SquareNumber sn = new SquareNumber();
Scanner sc = new Scanner(System.in);
System.out.println("enter the number value :");
int n = sc.nextInt();
System.out.println("The square of a number is :- " + sn.test(n));
}
}
Output :-
enter the number value :
12
The square of a number is :- 144