Square Number in Java Program

We will learn how to find square a number from given user input.

import java.util.Sanner;
class SquareNumber
{
	public static void main(String ar[])
	{
	  Scanner sc=new Scanner(System.in);
	  int num, sq;
	  System.out.print("Enter a number: ");
	  num=sc.nextInt();
	  sq = num*num;
	  System.out.println("Square Number is: "+ sq);
	}
}
Enter a number: 2
Square Number is: 4
  1. We create two variables num and sq.
  2. take a integer number from user and store it num variable.
  3. After that calculate square number by multiplying the number by itself and store it sq variable.
  4. and then print square number that store sq variable.

Be the first to comment

Leave a Reply

Your email address will not be published.


*