Write a program that calculates and displays the area of a rectangle.

Solution

The area of a rectangle can be calculated using the following formula:

Area = Base × Height

In this exercise, the user enters values for Base and Height and the program (or script) calculates and displays the area of the rectangle. The solution to this problem is shown here.

PHP

<?php
  echo "Enter the length of Base: ";
  $base_parallelogram = trim(fgets(STDIN));
  echo "Enter the length of Height: ";
  $height_parallelogram = trim(fgets(STDIN));

  $area = $base_parallelogram * $height_parallelogram;

  echo "The area of the parallelogram is ", $area;
?>

Java

public static void main(String[] args) throws java.io.IOException {
  java.io.BufferedReader cin = new java.io.
          BufferedReader(new java.io.InputStreamReader(System.in));
  double area, base_parallelogram, height_parallelogram;

  System.out.print("Enter the length of Base: ");
  base_parallelogram = Double.parseDouble(cin.readLine());
  System.out.print("Enter the length of Height: ");
  height_parallelogram = Double.parseDouble(cin.readLine());

  area = base_parallelogram * height_parallelogram;

  System.out.println("The area of the parallelogram is " + area);
}

C++

#include <iostream>
using namespace std;
int main() {
  double area, base_parallelogram, height_parallelogram;

  cout << "Enter the length of Base: ";
  cin >> base_parallelogram;
  cout << "Enter the length of Height: ";
  cin >> height_parallelogram;

  area = base_parallelogram * height_parallelogram;

  cout << "The area of the parallelogram is " << area;
  return 0;
}

C#

static void Main() {
  double area, base_parallelogram, height_parallelogram;

  Console.Write("Enter the length of Base: ");
  base_parallelogram = Double.Parse(Console.ReadLine());
  Console.Write("Enter the length of Height: ");
  height_parallelogram = Double.Parse(Console.ReadLine());

  area = base_parallelogram * height_parallelogram;

  Console.Write("The area of the parallelogram is " + area);
  Console.ReadKey();
}

Visual Basic

Sub Main()
  Dim area, base_parallelogram, height_parallelogram As Double

  Console.Write("Enter the length of Base: ")
  base_parallelogram = Console.ReadLine()
  Console.Write("Enter the length of Height: ")
  height_parallelogram = Console.ReadLine()

  area = base_parallelogram * height_parallelogram

  Console.Write("The area of the parallelogram is " & area)

  Console.ReadKey()
End Sub

Python

base_parallelogram = float(input("Enter the length of Base: "))
height_parallelogram = float(input("Enter the length of Height: "))

area = base_parallelogram* height_parallelogram

print("The area of the parallelogram is", area)

Leave a Reply

Your email address will not be published. Required fields are marked *

This article is taken from a book of mine. If you enjoyed it, please visit my author's page inand check out all of my books!