Posted on by Kalkicode
Code Geometric

Calculate area of rectangle

Calculates the area of a rectangle based on its width and height. The area of a rectangle is a basic geometric concept used in various applications, such as construction, architecture, and mathematics. The formula to calculate the area of a rectangle is width * height.

Problem Statement

Given the width and height of a rectangle, the task is to calculate and display its corresponding area.

Example

For a rectangle with a width of 5 units and a height of 10 units, the area can be calculated using the formula: width * height = 5 * 10 = 50 square units.

Solution Idea

The solution involves using the formula for calculating the area of a rectangle and displaying the result.

Pseudocode

rectangle_area(width, height):
    area = width * height
    display rectangle size [width, height]
    display area

Algorithm Explanation

  1. The rectangle_area function takes the width and height of a rectangle as inputs.
  2. It calculates the area of the rectangle using the formula width * height.
  3. The calculated area is displayed along with the provided width and height.

Code Solution

// C Program
// Calculate area of rectangle 
#include <stdio.h>

//Calculate area of rectangle by given width and height
void rectangle_area(double width, double height)
{
  //Calculate area of rectangle
  double area = width * height;
  //Display the result of rectangle area
  printf(" Rectangle Size [ width : %lf, height : %lf ] ", width, height);
  printf("\n Rectangle Area : %lf\n\n", area);
}
int main()
{
  //Test Cases  
  rectangle_area(10, 20);
  rectangle_area(15.5, 13.4);
  rectangle_area(20, 20);
  rectangle_area(12.6, 25);
  return 0;
}

Output

 Rectangle Size [ width : 10.000000, height : 20.000000 ]
 Rectangle Area : 200.000000

 Rectangle Size [ width : 15.500000, height : 13.400000 ]
 Rectangle Area : 207.700000

 Rectangle Size [ width : 20.000000, height : 20.000000 ]
 Rectangle Area : 400.000000

 Rectangle Size [ width : 12.600000, height : 25.000000 ]
 Rectangle Area : 315.000000
/*
  C++ program
  Calculate area of rectangle 
*/
#include<iostream>

using namespace std;
class RectangleArea
{
  public:
    //Calculate area of rectangle by given width and height
    void rectangle_area(double width, double height)
    {
      //Calculate area of rectangle
      double area = width *height;
      cout << " Rectangle Size [ width : " << width << ", height : " << height << " ] ";
      cout << "\n Rectangle Area : " << area << "\n\n";
    }
};
int main()
{
  RectangleArea obj = RectangleArea();
  //Test Cases  
  obj.rectangle_area(10, 20);
  obj.rectangle_area(15.5, 13.4);
  obj.rectangle_area(20, 20);
  obj.rectangle_area(12.6, 25);
  return 0;
}

Output

 Rectangle Size [ width : 10, height : 20 ]
 Rectangle Area : 200

 Rectangle Size [ width : 15.5, height : 13.4 ]
 Rectangle Area : 207.7

 Rectangle Size [ width : 20, height : 20 ]
 Rectangle Area : 400

 Rectangle Size [ width : 12.6, height : 25 ]
 Rectangle Area : 315
/*
  Java program
  Calculate area of rectangle 
*/
public class RectangleArea
{
  //Calculate area of rectangle by given width and height
  public void rectangle_area(double width, double height)
  {
    //Calculate area of rectangle
    double area = width * height;
    //Display the result of rectangle area
    System.out.print(" Rectangle Size [ width : " + width + ", height : " + height + " ] ");
    System.out.print("\n Rectangle Area : " + area + "\n\n");
  }
  public static void main(String[] args)
  {
    RectangleArea obj = new RectangleArea();
    //Test Cases  
    obj.rectangle_area(10, 20);
    obj.rectangle_area(15.5, 13.4);
    obj.rectangle_area(20, 20);
    obj.rectangle_area(12.6, 25);
  }
}

Output

 Rectangle Size [ width : 10.0, height : 20.0 ] 
 Rectangle Area : 200.0

 Rectangle Size [ width : 15.5, height : 13.4 ] 
 Rectangle Area : 207.70000000000002

 Rectangle Size [ width : 20.0, height : 20.0 ] 
 Rectangle Area : 400.0

 Rectangle Size [ width : 12.6, height : 25.0 ] 
 Rectangle Area : 315.0
/*
  C# program
  Calculate area of rectangle 
*/
using System;
public class RectangleArea
{
  //Calculate area of rectangle by given width and height
  public void rectangle_area(double width, double height)
  {
    //Calculate area of rectangle
    double area = width * height;
    Console.Write(" Rectangle Size [ width : " + width + ", height : " + height + " ] ");
    Console.Write("\n Rectangle Area : " + area + "\n\n");
  }
  public static void Main(String[] args)
  {
    RectangleArea obj = new RectangleArea();
    //Test Cases  
    obj.rectangle_area(10, 20);
    obj.rectangle_area(15.5, 13.4);
    obj.rectangle_area(20, 20);
    obj.rectangle_area(12.6, 25);
  }
}

Output

 Rectangle Size [ width : 10, height : 20 ]
 Rectangle Area : 200

 Rectangle Size [ width : 15.5, height : 13.4 ]
 Rectangle Area : 207.7

 Rectangle Size [ width : 20, height : 20 ]
 Rectangle Area : 400

 Rectangle Size [ width : 12.6, height : 25 ]
 Rectangle Area : 315
<?php
/*
  Php program
  Calculate area of rectangle 
*/
class RectangleArea
{
  //Calculate area of rectangle by given width and height
  public  function rectangle_area($width, $height)
  {
    //Calculate area of rectangle
    $area = $width *$height;
    //Display the result of rectangle area
    echo(" Rectangle Size [ width : ". $width .", height : ". $height ." ] ");
    echo("\n Rectangle Area : ". $area ."\n\n");
  }
}

function main()
{
  $obj = new RectangleArea();
  //Test Cases  
  $obj->rectangle_area(10, 20);
  $obj->rectangle_area(15.5, 13.4);
  $obj->rectangle_area(20, 20);
  $obj->rectangle_area(12.6, 25);
}
main();

Output

 Rectangle Size [ width : 10, height : 20 ]
 Rectangle Area : 200

 Rectangle Size [ width : 15.5, height : 13.4 ]
 Rectangle Area : 207.7

 Rectangle Size [ width : 20, height : 20 ]
 Rectangle Area : 400

 Rectangle Size [ width : 12.6, height : 25 ]
 Rectangle Area : 315
/*
  Node Js program
  Calculate area of rectangle 
*/
class RectangleArea
{
  //Calculate area of rectangle by given width and height
  rectangle_area(width, height)
  {
    //Calculate area of rectangle
    var area = width *height;
    //Display the result of rectangle area
    process.stdout.write(" Rectangle Size [ width : " + width + ", height : " + height + " ] ");
    process.stdout.write("\n Rectangle Area : " + area + "\n\n");
  }
}

function main(args)
{
  var obj = new RectangleArea();
  //Test Cases  
  obj.rectangle_area(10, 20);
  obj.rectangle_area(15.5, 13.4);
  obj.rectangle_area(20, 20);
  obj.rectangle_area(12.6, 25);
}
main();

Output

 Rectangle Size [ width : 10, height : 20 ]
 Rectangle Area : 200

 Rectangle Size [ width : 15.5, height : 13.4 ]
 Rectangle Area : 207.70000000000002

 Rectangle Size [ width : 20, height : 20 ]
 Rectangle Area : 400

 Rectangle Size [ width : 12.6, height : 25 ]
 Rectangle Area : 315
#   Python 3 program
#   Calculate area of rectangle 

class RectangleArea :
  # Calculate area of rectangle by given width and height
  def rectangle_area(self, width, height) :
    # Calculate area of rectangle
    area = width * height
    
      # Display the result of rectangle area
    print(" Rectangle Size [ width : ", width ,", height : ", height ," ] ", end = "")
    print("\n Rectangle Area : ", area ,"\n\n", end = "")
  

def main() :
  obj = RectangleArea()
  # Test Cases  
  obj.rectangle_area(10, 20)
  obj.rectangle_area(15.5, 13.4)
  obj.rectangle_area(20, 20)
  obj.rectangle_area(12.6, 25)


if __name__ == "__main__": main()

Output

 Rectangle Size [ width :  10 , height :  20  ]
 Rectangle Area :  200

 Rectangle Size [ width :  15.5 , height :  13.4  ]
 Rectangle Area :  207.70000000000002

 Rectangle Size [ width :  20 , height :  20  ]
 Rectangle Area :  400

 Rectangle Size [ width :  12.6 , height :  25  ]
 Rectangle Area :  315.0
#   Ruby program
#   Calculate area of rectangle 

class RectangleArea

  # Calculate area of rectangle by given width and height
  def rectangle_area(width, height)
  
    # Calculate area of rectangle
    area = width * height
    # Display the result of rectangle area
    print(" Rectangle Size [ width  : ", width ,", height  : ", height ," ] ")
    print("\n Rectangle Area  : ", area ,"\n\n")
  end
end
def main()

  obj = RectangleArea.new()
  # Test Cases  
  obj.rectangle_area(10, 20)
  obj.rectangle_area(15.5, 13.4)
  obj.rectangle_area(20, 20)
  obj.rectangle_area(12.6, 25)
end
main()

Output

 Rectangle Size [ width  : 10, height  : 20 ] 
 Rectangle Area  : 200

 Rectangle Size [ width  : 15.5, height  : 13.4 ] 
 Rectangle Area  : 207.70000000000002

 Rectangle Size [ width  : 20, height  : 20 ] 
 Rectangle Area  : 400

 Rectangle Size [ width  : 12.6, height  : 25 ] 
 Rectangle Area  : 315.0

/*
  Scala program
  Calculate area of rectangle 
*/
class RectangleArea
{
  //Calculate area of rectangle by given width and height
  def rectangle_area(width: Double, height: Double): Unit = {
    //Calculate area of rectangle
    var area: Double = width * height;
    //Display the result of rectangle area
    print(" Rectangle Size [ width : " + width + ", height : " + height + " ] ");
    print("\n Rectangle Area : " + area + "\n\n");
  }
}
object Main
{
  def main(args: Array[String]): Unit = {
    var obj: RectangleArea = new RectangleArea();
    //Test Cases  
    obj.rectangle_area(10, 20);
    obj.rectangle_area(15.5, 13.4);
    obj.rectangle_area(20, 20);
    obj.rectangle_area(12.6, 25);
  }
}

Output

 Rectangle Size [ width : 10.0, height : 20.0 ]
 Rectangle Area : 200.0

 Rectangle Size [ width : 15.5, height : 13.4 ]
 Rectangle Area : 207.70000000000002

 Rectangle Size [ width : 20.0, height : 20.0 ]
 Rectangle Area : 400.0

 Rectangle Size [ width : 12.6, height : 25.0 ]
 Rectangle Area : 315.0
/*
  Swift program
  Calculate area of rectangle 
*/
class RectangleArea
{
  //Calculate area of rectangle by given width and height
  func rectangle_area(_ width: Double, _ height: Double)
  {
    //Calculate area of rectangle
    let area: Double = width * height;
    
      //Display the result of rectangle area
    print(" Rectangle Size [ width : ", width ,", height : ", height ," ] ", terminator: "");
    print("\n Rectangle Area : ", area ,"\n\n", terminator: "");
  }
}
func main()
{
  let obj: RectangleArea = RectangleArea();
  //Test Cases  
  obj.rectangle_area(10, 20);
  obj.rectangle_area(15.5, 13.4);
  obj.rectangle_area(20, 20);
  obj.rectangle_area(12.6, 25);
}
main();

Output

 Rectangle Size [ width :  10.0 , height :  20.0  ]
 Rectangle Area :  200.0

 Rectangle Size [ width :  15.5 , height :  13.4  ]
 Rectangle Area :  207.7

 Rectangle Size [ width :  20.0 , height :  20.0  ]
 Rectangle Area :  400.0

 Rectangle Size [ width :  12.6 , height :  25.0  ]
 Rectangle Area :  315.0

Output Explanation

The output of the program displays the calculated area for each provided width and height combination. For example, when the width is 10 units and the height is 20 units, the area is calculated as 200 square units.

Time Complexity

The time complexity of the algorithm is constant O(1) because the calculation involves basic arithmetic operations. The time taken to calculate the area is not affected by the values of width and height.

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