Java Program to calculate the perimeter of a rhombus – In this distinct article, we will brief in on the multiple methods of calculating the perimeter of a rhombus.
Suitable examples and sample programs have been added to the blog for better understanding. The compiler has also been added so that you can execute it yourself.
The ways used in this piece to calculate the perimeter of a rhombus are as follows:
- Using Scanner Class
- Using Command Line Arguments
- Using Static Function
As we all know, a rhombus is a two-dimensional quadrilateral figure. It is intensively used in the field of geometry.
The sides of a rhombus are all equal in nature. The opposite sides of a rhombus are parallel to each other. The opposite angles of a rhombus are equal to each other.
The rhombus is basically a parallelogram whose adjacent sides are equal to each other.
As you can see, this is a rhombus with a side length of 4cm and a height of 2cm.
So, the perimeter of a rhombus is:
P = a + a + a + a = 4a
Hence, the perimeter of this rhombus is as follows:
P = 4a = 4*4 = 16cm.
Thus, the multiple methods used to calculate the perimeter of a rhombus 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 |
import java.util.Scanner; class PerimeterOfRhombus { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the side of the Rhombus:"); double a= s.nextDouble(); double perimeter=4*a; System.out.println("perimeter of Rhombus is: " + perimeter); } } |
1 2 |
Enter the side of the Rhombus:4 perimeter of Rhombus is: 16.0 |
Using Command Line Arguments
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Scanner; class PerimeterOfRhombus1 { public static void main(String args[]) { double a= Double.parseDouble(args[0]); double perimeter=4*a; System.out.println("perimeter of Rhombus is: " + perimeter); } } |
1 2 3 |
javac PerimeterOfRhombus1.java java PerimeterOfRhombus1 4 perimeter of Rhombus is: 16.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 |
import java.util.Scanner; class PerimeterOfRhombus { public static void main(String args[]) { Scanner s= new Scanner(System.in); System.out.println("Enter the side of the Rhombus:"); double a= s.nextDouble(); double perimeter=PerimeterOfRhombus.area(a); System.out.println("perimeter of Rhombus is: " + perimeter); } public static double area(double l) { double a=4*l; return a; } } |
1 2 |
Enter the side of the Rhombus:4 perimeter of Rhombus is: 16.0 |