This is the Java Program to Find the Area of a Parallelogram.

Problem Description

Given the dimensions of a parallelogram, find out its area.

Problem Solution

The area of a parallelogram can be calculated using the formula:

**Area = base*height.
**

Program/Source Code

Here is the source code of the Java Program to Find the Area of a Parallelogram. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

`//Java Program to Find the Area of a Parallelogram`

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class AreaOfAParallelogram {

`// Function to calculate the area of a parallelogram`

public static void main(String[] args) {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

double base,height;

System.out.println("Enter the base and the height of the parallelogram");

try{

base=Double.parseDouble(br.readLine());

height=Double.parseDouble(br.readLine());

}catch (Exception e){

System.out.println("An error occurred");

return;

`}`

if(base<=0 || height<=0){

System.out.println("Wrong Input");

return;

`}`

System.out.println("Area = " + base*height );

`}`

`}`

Program Explanation

1. In function main(), first, the base and height of the parallelogram are taken as input.

2. The condition if(base<=0 || height<=0) checks if the input is valid or not.

3. The print statement displays the area of the parallelogram.

advertisement

advertisement

**Time Complexity: O(1)**

Runtime Test Cases

Case 1 (Simple Test Case): Enter the base and the height of the parallelogram 8.34 5.678 Area = 47.35452

**Sanfoundry Global Education & Learning Series – Java Programs.**

**If you find any mistake above, kindly email to [email protected]**

**Related Posts:**

- Practice BCA MCQs
- Apply for Java Internship
- Practice Programming MCQs
- Check Programming Books
- Apply for Computer Science Internship