In the previous article we have discussed Java Program to Find Area and Perimeter of Rectangle
In this article we will discuss about how to find area and perimeter of a square.
Program to Find Area and Perimeter of Square
Before going into the program, let’s see how we find the area and perimeter of a square.
Formula for area of Square = side*side
Formula for perimeter of Square = 4*side
Where,
- side*side represents the
length*breadth
of the square. (each side of square is same) 4*side
represents the4*length of side
of the square. (each side of square is same)
Example:
Example- To find Area of Square When length of each side of square = 4 Then area of square => area = side*side => area = 4*4 => area = 16
Example- To find Perimeter of Square When length of each side of square = 4 Then area of square => perimeter = 4*side => perimeter = 4*4 => perimeter =16
Now, let’s see the program.
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Method-1: Java Program to Find Area and Perimeter of Square By Using Static Values
In this method, the side length of square is already declared in the program and stored by the variable sideLength
. Which will be used to calculate the area and perimeter of square by using the formula.
import java.util.*; public class JavaProgram { public static void main(String args[]) { int sideLength; // length of each side of square is given sideLength = 5; //finding area of square int area = sideLength*sideLength; //finding perimeter of square int perimeter = 4*sideLength; System.out.print("Area of Square = " +area); System.out.print("\nPerimeter of Square = " +perimeter); } }
Output: Area of Square = 25 Perimeter of Square = 20
Method-2: Java Program to Find Area and Perimeter of Square By Using Dynamic Values
In this method, the side length of square will be taken as user input and then the area and perimeter of square will be calculated as per method-1. Method-1 and Method-2 is same the difference is only in assigning side length value of square.
import java.util.*; public class JavaProgram { public static void main(String args[]) { int side, perimeter, area; //Scanner class object created fro taking user input Scanner sc = new Scanner(System.in); //taking the length of any side of square from the user System.out.print("Enter length of any side of square : "); side = sc.nextInt(); //finding area of square area = side*side; //finding perimeter of square perimeter = 4*side; System.out.print("Area of Square = " +area); System.out.print("\nPerimeter of Square = " +perimeter); } }
Output: Enter length of any side of square : 4 Area of Square = 16 Perimeter of Square = 16
Method-3: Java Program to Find Area and Perimeter of Square By Using User Defined Method
In this method, first the side length of square will be taken by the user and this value will be passed to the user defined method calculate()
where the logic to find area and perimeter is implemented.
import java.util.*; public class JavaProgram { public static void main(String args[]) { int side; //Scanner class object created fro taking user input Scanner sc = new Scanner(System.in); //taking the length of any side of square from the user System.out.print("Enter length of any side of square : "); side = sc.nextInt(); calculate(side); } public static void calculate(int sideLength) { //finding area of square int area = sideLength*sideLength; //finding perimeter of square int perimeter = 4*sideLength; System.out.print("Area of Square = " +area); System.out.print("\nPerimeter of Square = " +perimeter); } }
Output: Enter length of any side of square : 4 Area of Square = 16 Perimeter of Square = 16
Explore complete java concepts from the Java programming examples and get ready to become a
good programmer and crack the java software developer interview with ease.
Answer these:
- Write a program in java to accept the diagonal of a square find and display the area and perimeter?
- Write a program in java to find the area perimeter and diagonal of a square?
- Write a java program to find the area and perimeter of square and circle using interface?
- Write a program to input the area of a square and find its perimeter?
- How to find the area and the perimeter of a square?
- How to get area and perimeter of a square?
- Write a java program to find the perimeter of a square?
- Java program to find area and perimeter of triangle?
- Program to find area of rectangle using inheritance in java?
- Java program to find area and perimeter of square?
- Java program to calculate area and perimeter of square?
- Area of square program in java using scanner?
- Safe square area program in java?
- Java program to find area of square?
Related Java Programs: