Write a Java Program to find Square of a Number using methods with example.
Java Program to Find Square of a Number Example 1
This Java program allows the user to enter an integer value. Then this Java program calculates the square of that number using Arithmetic Operator.
// Java Program to Find Square of a Number import java.util.Scanner; public class SquareofNumber { private static Scanner sc; public static void main(String[] args) { int number, square; sc = new Scanner(System.in); System.out.print(" Please Enter any Number : "); number = sc.nextInt(); square = number * number; System.out.println("\n The Square of a Given Number " + number + " = " + square); } }
Java Program to Find Square of a Number Example 2
This Java program is the same as above. But this time, we are creating a separate Java method to calculate the square of that number.
// Java Program to Find Square of a Number import java.util.Scanner; public class SquareofNumber1 { private static Scanner sc; public static void main(String[] args) { int number, square; sc = new Scanner(System.in); System.out.print(" Please Enter any Number : "); number = sc.nextInt(); // Calling below static method calsqaure square = calsquare(number); System.out.println("\n The Square of a Given Number " + number + " = " + square); } public static int calsquare(int num) { return num * num; } }
Java square of a number output
Please Enter any Number : 64
The Square of a Given Number 64 = 4096
Comments are closed.