Thursday, December 26, 2024
Google search engine
HomeLanguagesJavascriptJavaScript program to find Area and Perimeter of Rectangle

JavaScript program to find Area and Perimeter of Rectangle

A rectangle is a flat figure in a plane. It has four sides and four equal angles of 90 degrees each. In a rectangle all four sides are not of equal length like a square, sides opposite to each other have equal length. Both diagonals of the rectangle have equal lengths.

Area And Perimeter Of Rectangle

Examples:

Input: 4 5
Output: Area = 20
        Perimeter = 18

Input: 2 3
Output: Area = 6
        Perimeter = 10

Formulae:

Area of rectangle: a*b 
Perimeter of rectangle: 2*(a + b) 

Example: Below is the example that will illustrate the above formula:

Javascript




<script>
    // Function to Find the Area of Triangle
    function areaRectangle(a, b) {
        let area = a * b;
        return area;
    }
 
    // Function to Find the Parameter of Triangle
    function perimeterRectangle(a, b) {
        let perimeter = 2 * (a + b);
        return perimeter;
    }
 
    // Driver program
    let a = 5;
    let b = 6;
    console.log("Area = " + areaRectangle(a, b));
    console.log("Perimeter = " + perimeterRectangle(a, b));
</script>


Output:

Area = 30
Perimeter = 22

Time Complexity: O(1) as it is performing constant operations
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments