Posted on by Kalkicode
Code Geometric

Find area of parallelogram

The problem involves calculating the area of a parallelogram using its base and altitude. A parallelogram is a quadrilateral with opposite sides that are parallel and equal in length. Calculating the area of a parallelogram is a fundamental geometric calculation used in various fields such as geometry, engineering, and architecture.

Problem Statement

Given the length of the base and the altitude (perpendicular distance between the base and the opposite side) of a parallelogram, we need to calculate and output the area of the parallelogram. The base and altitude are provided as inputs, and we are required to use the formula for the area of a parallelogram to find the area.

Example

Consider a parallelogram with a base of 8.8 and an altitude of 3.9. This parallelogram can be visualized as follows:


     /\
    /  \
   /    \
  /______\
 8.8        3.9

Idea to Solve

To solve this problem, we can follow these steps:

  1. Calculate the area of the parallelogram using the formula: area = base * altitude.
  2. Use the provided values for the base and altitude to compute the area.

Pseudocode

function parallelogram_area(altitude, base):
    area = base * altitude
    return area

for each test case:
    altitude, base = parallelogram parameters
    result = parallelogram_area(altitude, base)
    print("Parallelogram Size [ base :", base, ", altitude :", altitude, "]")
    print("Parallelogram Area :", result)

Algorithm Explanation

  1. For each test case, calculate the area of the parallelogram using the provided formula.
  2. Print the parallelogram's dimensions (base and altitude) and the calculated area.

Code Solution

// C Program
// Find area of parallelogram
#include <stdio.h>

//Calculate area of parallelogram by given altitude and base
void parallelogram_area(double altitude, double base)
{
	//Calculate area of parallelogram
	double area = base * altitude;
	//Display the result of parallelogram area
	printf(" Parallelogram Size [ base : %lf, altitude : %lf ] ", base, altitude);
	printf("\n Parallelogram Area : %lf\n\n", area);
}
int main()
{
	//Test Cases  
	parallelogram_area(3.9, 8.8);
	parallelogram_area(5.1, 11.2);
	parallelogram_area(4, 10);
	parallelogram_area(1.6, 2);
	return 0;
}

Output

 Parallelogram Size [ base : 8.800000, altitude : 3.900000 ]
 Parallelogram Area : 34.320000

 Parallelogram Size [ base : 11.200000, altitude : 5.100000 ]
 Parallelogram Area : 57.120000

 Parallelogram Size [ base : 10.000000, altitude : 4.000000 ]
 Parallelogram Area : 40.000000

 Parallelogram Size [ base : 2.000000, altitude : 1.600000 ]
 Parallelogram Area : 3.200000
/*
  Java program
  Find area of parallelogram
*/
public class Parallelogram
{
	//Calculate area of parallelogram by given altitude and base
	public void parallelogram_area(double altitude, double base)
	{
		//Calculate area of parallelogram
		double area = base * altitude;
		//Display the result of parallelogram area
		System.out.print(" Parallelogram Size [ base : " + base + ", altitude : " + altitude + " ] ");
		System.out.print("\n Parallelogram Area : " + area + "\n\n");
	}
	public static void main(String[] args)
	{
		Parallelogram obj = new Parallelogram();
		//Test Cases  
		obj.parallelogram_area(3.9, 8.8);
		obj.parallelogram_area(5.1, 11.2);
		obj.parallelogram_area(4, 10);
		obj.parallelogram_area(1.6, 2);
	}
}

Output

 Parallelogram Size [ base : 8.8, altitude : 3.9 ] 
 Parallelogram Area : 34.32

 Parallelogram Size [ base : 11.2, altitude : 5.1 ] 
 Parallelogram Area : 57.11999999999999

 Parallelogram Size [ base : 10.0, altitude : 4.0 ] 
 Parallelogram Area : 40.0

 Parallelogram Size [ base : 2.0, altitude : 1.6 ] 
 Parallelogram Area : 3.2
/*
  C++ program
  Find area of parallelogram
*/
#include<iostream>

using namespace std;
class Parallelogram
{
	public:
		//Calculate area of parallelogram by given altitude and base
		void parallelogram_area(double altitude, double base)
		{
			//Calculate area of parallelogram
			double area = base *altitude;
			cout << " Parallelogram Size [ base : " << base << ", altitude : " << altitude << " ] ";
			cout << "\n Parallelogram Area : " << area << "\n\n";
		}
};
int main()
{
	Parallelogram obj =  Parallelogram();
	//Test Cases  
	obj.parallelogram_area(3.9, 8.8);
	obj.parallelogram_area(5.1, 11.2);
	obj.parallelogram_area(4, 10);
	obj.parallelogram_area(1.6, 2);
	return 0;
}

Output

 Parallelogram Size [ base : 8.8, altitude : 3.9 ]
 Parallelogram Area : 34.32

 Parallelogram Size [ base : 11.2, altitude : 5.1 ]
 Parallelogram Area : 57.12

 Parallelogram Size [ base : 10, altitude : 4 ]
 Parallelogram Area : 40

 Parallelogram Size [ base : 2, altitude : 1.6 ]
 Parallelogram Area : 3.2
/*
  C# program
  Find area of parallelogram
*/
using System;
public class Parallelogram
{
	//Calculate area of parallelogram by given altitude and base
	public void parallelogram_area(double altitude, double base_side)
	{
		//Calculate area of parallelogram
		double area = base_side * altitude;
		Console.Write(" Parallelogram Size [ base : " + base_side + ", altitude : " + altitude + " ] ");
		Console.Write("\n Parallelogram Area : " + area + "\n\n");
	}
	public static void Main(String[] args)
	{
		Parallelogram obj = new Parallelogram();
		//Test Cases  
		obj.parallelogram_area(3.9, 8.8);
		obj.parallelogram_area(5.1, 11.2);
		obj.parallelogram_area(4, 10);
		obj.parallelogram_area(1.6, 2);
	}
}

Output

 Parallelogram Size [ base : 8.8, altitude : 3.9 ]
 Parallelogram Area : 34.32

 Parallelogram Size [ base : 11.2, altitude : 5.1 ]
 Parallelogram Area : 57.12

 Parallelogram Size [ base : 10, altitude : 4 ]
 Parallelogram Area : 40

 Parallelogram Size [ base : 2, altitude : 1.6 ]
 Parallelogram Area : 3.2
<?php
/*
  Php program
  Find area of parallelogram
*/
class Parallelogram
{
	//Calculate area of parallelogram by given altitude and base
	public 	function parallelogram_area($altitude, $base)
	{
		//Calculate area of parallelogram
		$area = $base *$altitude;
		//Display the result of parallelogram area
		echo(" Parallelogram Size [ base : ". $base .", altitude : ". $altitude ." ] ");
		echo("\n Parallelogram Area : ". $area ."\n\n");
	}
}

function main()
{
	$obj = new Parallelogram();
	//Test Cases  
	$obj->parallelogram_area(3.9, 8.8);
	$obj->parallelogram_area(5.1, 11.2);
	$obj->parallelogram_area(4, 10);
	$obj->parallelogram_area(1.6, 2);
}
main();

Output

 Parallelogram Size [ base : 8.8, altitude : 3.9 ]
 Parallelogram Area : 34.32

 Parallelogram Size [ base : 11.2, altitude : 5.1 ]
 Parallelogram Area : 57.12

 Parallelogram Size [ base : 10, altitude : 4 ]
 Parallelogram Area : 40

 Parallelogram Size [ base : 2, altitude : 1.6 ]
 Parallelogram Area : 3.2
/*
  Node Js program
  Find area of parallelogram
*/
class Parallelogram
{
	//Calculate area of parallelogram by given altitude and base
	parallelogram_area(altitude, base)
	{
		//Calculate area of parallelogram
		var area = base *altitude;
		//Display the result of parallelogram area
		process.stdout.write(" Parallelogram Size [ base : " + base + ", altitude : " + altitude + " ] ");
		process.stdout.write("\n Parallelogram Area : " + area + "\n\n");
	}
}

function main(args)
{
	var obj = new Parallelogram();
	//Test Cases  
	obj.parallelogram_area(3.9, 8.8);
	obj.parallelogram_area(5.1, 11.2);
	obj.parallelogram_area(4, 10);
	obj.parallelogram_area(1.6, 2);
}
main();

Output

 Parallelogram Size [ base : 8.8, altitude : 3.9 ]
 Parallelogram Area : 34.32

 Parallelogram Size [ base : 11.2, altitude : 5.1 ]
 Parallelogram Area : 57.11999999999999

 Parallelogram Size [ base : 10, altitude : 4 ]
 Parallelogram Area : 40

 Parallelogram Size [ base : 2, altitude : 1.6 ]
 Parallelogram Area : 3.2
#   Python 3 program
#   Find area of parallelogram

class Parallelogram :
	# Calculate area of parallelogram by given altitude and base
	def parallelogram_area(self, altitude, base) :
		# Calculate area of parallelogram
		area = base * altitude
		
		# Display the result of parallelogram area
		print(" Parallelogram Size [ base : ", base ,", altitude : ", altitude ," ] ", end = "")
		print("\n Parallelogram Area : ", area ,"\n\n", end = "")
	

def main() :
	obj = Parallelogram()
	# Test Cases  
	obj.parallelogram_area(3.9, 8.8)
	obj.parallelogram_area(5.1, 11.2)
	obj.parallelogram_area(4, 10)
	obj.parallelogram_area(1.6, 2)


if __name__ == "__main__": main()

Output

 Parallelogram Size [ base :  8.8 , altitude :  3.9  ]
 Parallelogram Area :  34.32

 Parallelogram Size [ base :  11.2 , altitude :  5.1  ]
 Parallelogram Area :  57.11999999999999

 Parallelogram Size [ base :  10 , altitude :  4  ]
 Parallelogram Area :  40

 Parallelogram Size [ base :  2 , altitude :  1.6  ]
 Parallelogram Area :  3.2
#   Ruby program
#   Find area of parallelogram

class Parallelogram

	# Calculate area of parallelogram by given altitude and base
	def parallelogram_area(altitude, base)
	
		# Calculate area of parallelogram
		area = base * altitude
		# Display the result of parallelogram area
		print(" Parallelogram Size { base  : ", base ,", altitude  : ", altitude ," } ")
		print("\n Parallelogram Area  :", area ,"\n\n")
	end
end
def main()

	obj = Parallelogram.new()
	# Test Cases  
	obj.parallelogram_area(3.9, 8.8)
	obj.parallelogram_area(5.1, 11.2)
	obj.parallelogram_area(4, 10)
	obj.parallelogram_area(1.6, 2)
end
main()

Output

 Parallelogram Size { base  : 8.8, altitude  : 3.9 } 
 Parallelogram Area  :34.32

 Parallelogram Size { base  : 11.2, altitude  : 5.1 } 
 Parallelogram Area  :57.11999999999999

 Parallelogram Size { base  : 10, altitude  : 4 } 
 Parallelogram Area  :40

 Parallelogram Size { base  : 2, altitude  : 1.6 } 
 Parallelogram Area  :3.2

/*
  Scala program
  Find area of parallelogram
*/
class Parallelogram
{
	//Calculate area of parallelogram by given altitude and base
	def parallelogram_area(altitude: Double, base: Double): Unit = {
		//Calculate area of parallelogram
		var area: Double = base * altitude;
		//Display the result of parallelogram area
		print(" Parallelogram Size [ base : " + base + ", altitude : " + altitude + " ] ");
		print("\n Parallelogram Area : " + area + "\n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: Parallelogram = new Parallelogram();
		//Test Cases  
		obj.parallelogram_area(3.9, 8.8);
		obj.parallelogram_area(5.1, 11.2);
		obj.parallelogram_area(4, 10);
		obj.parallelogram_area(1.6, 2);
	}
}

Output

 Parallelogram Size [ base : 8.8, altitude : 3.9 ]
 Parallelogram Area : 34.32

 Parallelogram Size [ base : 11.2, altitude : 5.1 ]
 Parallelogram Area : 57.11999999999999

 Parallelogram Size [ base : 10.0, altitude : 4.0 ]
 Parallelogram Area : 40.0

 Parallelogram Size [ base : 2.0, altitude : 1.6 ]
 Parallelogram Area : 3.2
/*
  Swift program
  Find area of parallelogram
*/
class Parallelogram
{
	//Calculate area of parallelogram by given altitude and base
	func parallelogram_area(_ altitude: Double, _ base: Double)
	{
		//Calculate area of parallelogram
		let area: Double = base * altitude;
		
		//Display the result of parallelogram area
		print(" Parallelogram Size [ base : ", base ,", altitude : ", altitude ," ] ", terminator: "");
		print("\n Parallelogram Area : ", area ,"\n\n", terminator: "");
	}
}
func main()
{
	let obj: Parallelogram = Parallelogram();
	//Test Cases  
	obj.parallelogram_area(3.9, 8.8);
	obj.parallelogram_area(5.1, 11.2);
	obj.parallelogram_area(4, 10);
	obj.parallelogram_area(1.6, 2);
}
main();

Output

 Parallelogram Size [ base :  8.8 , altitude :  3.9  ]
 Parallelogram Area :  34.32

 Parallelogram Size [ base :  11.2 , altitude :  5.1  ]
 Parallelogram Area :  57.12

 Parallelogram Size [ base :  10.0 , altitude :  4.0  ]
 Parallelogram Area :  40.0

 Parallelogram Size [ base :  2.0 , altitude :  1.6  ]
 Parallelogram Area :  3.2

Output Explanation

The provided code runs four test cases and calculates the areas of parallelograms with different base lengths and altitudes. The output includes the parallelogram's dimensions and the corresponding calculated areas.

Time Complexity

The time complexity of this code is O(1) for each test case. The calculations involve basic arithmetic operations, which take constant time. Therefore, the overall time complexity remains constant for each test case.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment