Java program to calculate the Perimeter of a parallelogram – In this specific article, we will detail in on the multiple methods to calculate the perimeter of a parallelogram in Java Programming.
Suitable examples and sample outputs are provided for the easy comprehension of the whole scheme of things. The compiler has also been added so that you can execute it yourself.
The methods used in this piece are as follows:
- Using Scanner Class
- Using Command Line Arguments
- Using Static Function
A parallelogram, as we all know, is a two-dimensional quadrilateral figure used in the world of geometry.
A parallelogram, as the name itself suggests, has its opposite sides equal and parallel. The opposite angles are also equal in nature.
A special case where all the angles subtended by a parallelogram are 90 degrees is regarded as a rectangle.
As you can see, this is a Parallelogram with the longer side of 8cm and the shorter side of 6cm. Since the opposite sides of a parallelogram are equal, the perimeter can be calculated with this formula:
P = 2(a + b)
Hence, the perimeter of this parallelogram is:
P = 2(8 + 6) = 28cm.
TThus, the various methods to calculate the perimeter of a parallelogram in Java Programming is as follows:
Using Scanner Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class PerimeterOfParallelogram { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the height of the Parallelogram:"); double h= s.nextDouble(); System.out.println("Enter the breadth of the Parallelogram:"); double b= s.nextDouble(); double perimeter=2*(h+b); System.out.println("perimeter of Parallelogram is: " + perimeter); } } |
1 2 3 |
Enter the height of the Parallelogram:3 Enter the breadth of the Parallelogram:4 perimeter of Parallelogram is: 14.0 |
Using Command Line Arguments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Scanner; class PerimeterOfParallelogram1 { public static void main(String args[]) { double h= Double.parseDouble(args[0]); double b= Double.parseDouble(args[1]); double perimeter=2*(h+b); System.out.println("perimeter of Parallelogram is: " + perimeter); } } |
1 2 3 |
javac PerimeterOfParallelogram1.java java PerimeterOfParallelogram1 3 4 perimeter of Parallelogram is: 14.0 |
Using Static Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.Scanner; class PerimeterOfParallelogram { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the height of the Parallelogram:"); double h= s.nextDouble(); System.out.println("Enter the breadth of the Parallelogram:"); double b= s.nextDouble(); double perimeter=PerimeterOfParallelogram.area(h,b); System.out.println("perimeter of Parallelogram is: " + perimeter); } public static double area(double h,double b) { double a=2*(h+b); return a; } } |
1 2 3 |
Enter the height of the Parallelogram:3 Enter the breadth of the Parallelogram:4 perimeter of Parallelogram is: 14.0 |