Java Program to Find the Area of a Parallelogram
In this tutorial, we will learn how to calculate the area of a parallelogram in java. A parallelogram is a simple quadrilateral with two pairs of parallel sides. The opposite or facing sides of a parallelogram are of equal length and the opposite angles of a parallelogram are of equal measure. The area of a parallelogram is a region covered by a parallelogram in a two-dimensional plane.
Let's see the examples below.
Input: Enter the base: 7
Enter the height: 8
Output: Area of the parallelogram: 56
Below is the pictorial representation of the same.
The above problem can be solved in the following ways:
Program 1: Find the Area of a Parallelogram
In this program, we will learn how to find the area of a parallelogram using the base and height formula.
Algorithm
- Start
- Create an instance of the Scanner class.
- Declare variables to store the value of the base and height of the parallelogram.
- Ask the user to initialize the variables.
- Declare another variable to store the area of the parallelogram.
- Use the base and height formula to calculate the area.
- Display the result.
- Stop.
Below is the code example in the Java language.
//Java Program to Calculate the Area of a Parallelogram
import java.util.Scanner;
public class Main
{
public static void main(String []args)
{
//Take input from the user
//Create an instance of the Scanner Class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the base of the parallelogram: ");
int base=sc.nextInt();
System.out.println("Enter the height of the parallelogram: ");
int height=sc.nextInt();
int area=base*height;
// display the area of a parallelogram
System.out.println("Area of the parallelogram = " + area);
}
}
Enter the base of the parallelogram: 10
Enter the height of the parallelogram: 20
Area of the parallelogram = 200
Program 2: Find the Area of a Parallelogram in Java
In this program, we will learn how to find the area of a parallelogram using trigonometry.
Algorithm
- Start
- Create an instance of the Scanner class.
- Declare variables to store the value of the sides of the parallelogram.
- Ask the user to initialize the variables.
- Declare another variable to store the angle between the sides of the parallelogram.
- Conver it into radians.
- Calculate the sine value of the angle.
- Use the trigonometric formula to calculate the area of the parallelogram,
- Print the value of the area of the parallelogram.
- Stop.
Below is the code example in the Java language.
//Java Program to Calculate the Area of a Parallelogram
import java.util.Scanner;
public class Main
{
public static void main(String []args)
{
//Take input from the user
//Create an instance of the Scanner Class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the sides of the parallelogram: ");
double a1=sc.nextDouble();
System.out.println("Enter the sides of the parallelogram: ");
double b1=sc.nextDouble();
System.out.println("Enter the angle between the sides of the parallelogram: ");
double a = sc.nextDouble();
// converting values to radians
double b = Math.toRadians(a);
double area=a1*b1*(Math.sin(b));
// display the area of parallelogram
System.out.println("Area of the parallelogram = " + area);
}
}
?
Enter the sides of the parallelogram: 3
Enter the sides of the parallelogram: 4
Enter the angle between the sides of the parallelogram: 90
Area of the parallelogram = 12.0
Program 3: Java Program to Find the Area of a Parallelogram in Java
In this program, we will learn how to find the area of a parallelogram using diagonals.
Algorithm
- Start
- Create an instance of the Scanner class.
- Declare variables to store the value of the diagonals of the parallelogram.
- Ask the user to initialize these variables.
- Declare another variable to store the angle between any two diagonals of the parallelogram.
- Ask the user to initialize the variable.
- Conver it into radians.
- Calculate the sine value of the angle.
- Use the diagonal formula to calculate the area of the parallelogram,
- Print the value of the area of the parallelogram.
- Stop.
Below is the code example in the Java language.
//Java Program to Calculate the Area of a Parallelogram
import java.util.Scanner;
public class Main
{
public static void main(String []args)
{
//Take input from the user
//Create an instance of the Scanner Class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first diagonal of the parallelogram: ");
double d1=sc.nextDouble();
System.out.println("Enter the second diagonal of the parallelogram: ");
double d2=sc.nextDouble();
System.out.println("Enter the angle between the diagonals of the parallelogram: ");
double a = sc.nextDouble();
// converting values to radians
double b = Math.toRadians(a);
double area=(d1*d2*(Math.sin(b)))/2;
// display the area of parallelogram
System.out.println("Area of the parallelogram = " + area);
}
}
Enter the first diagonal of the parallelogram: 30
Enter the second diagonal of the parallelogram: 40
Enter the angle between the diagonals of the parallelogram: 30
Area of the parallelogram = 299.99999999999994