Posted on by Kalkicode
Code Geometric

# Calculate area of square

The problem revolves around calculating the area of a square given the length of its side. A square is a four-sided polygon in which all sides are of equal length, and all angles are right angles (90 degrees). Calculating the area of a square is a basic geometric computation used in various fields such as mathematics, engineering, and architecture.

## Problem Statement

Given the length of a side of a square, we need to calculate and output the area of the square. The side length is provided as input, and we are required to use the formula for the area of a square to find the area.

## Example

Consider a square with a side length of 5 units. This square can be visualized as follows:

_______
|       |
|       |
|       |
|_______|
5.0

## Idea to Solve

To solve this problem, we can follow these steps:

1. Calculate the area of the square using the formula: area = side * side.
2. Use the provided value for the side length to compute the area.

## Pseudocode

function square_area(side):
area = side * side
return area

for each test case:
side = square side length
result = square_area(side)
print("Square [ side :", side, "]")
print("Area :", result)

## Algorithm Explanation

1. For each test case, calculate the area of the square using the provided formula.
2. Print the square's side length and the calculated area.

## Code Solution

//C Program
//Calculate Area of Square
#include <stdio.h>

///Calculate area of Square by given side
void square_area(double side)
{
//Calculate area of square
double area = (side * side);
//Display the result of Square area
printf(" Square  [ side : %lf ] ", side);
printf("\n Area : %lf\n\n", area);
}
int main()
{
//Test Cases
square_area(5);
square_area(8);
square_area(7.5);
return 0;
}

#### Output

Square  [ side : 5.000000 ]
Area : 25.000000

Square  [ side : 8.000000 ]
Area : 64.000000

Square  [ side : 7.500000 ]
Area : 56.250000
// Java Program
// Calculate Area of Square
class SquareSize
{
///Calculate area of square by given side
public void square_area(double side)
{
//Calculate area of square
double area = (side * side);
//Display the result of square area
System.out.print(" Square [ side : " + side + " ] ");
System.out.print("\n Area : " + area + "\n\n");
}
public static void main(String[] args)
{
SquareSize obj = new SquareSize();
//Test Cases
obj.square_area(5);
obj.square_area(8);
obj.square_area(7.5);
}
}

#### Output

Square [ side : 5.0 ]
Area : 25.0

Square [ side : 8.0 ]
Area : 64.0

Square [ side : 7.5 ]
Area : 56.25
// C++ Program
// Calculate Area of Square
#include<iostream>

using namespace std;
class SquareSize
{
public:
///Calculate area of square by given side
void square_area(double side)
{
//Calculate area of square
double area = (side *side);
cout << " Square [ side : " << side << " ] ";
cout << "\n Area : " << area << "\n\n";
}
};
int main()
{
SquareSize obj = SquareSize();
//Test Cases
obj.square_area(5);
obj.square_area(8);
obj.square_area(7.5);
return 0;
}

#### Output

Square [ side : 5 ]
Area : 25

Square [ side : 8 ]
Area : 64

Square [ side : 7.5 ]
Area : 56.25
// C# Program
// Calculate Area of Square
using System;
class SquareSize
{
///Calculate area of square by given side
public void square_area(double side)
{
//Calculate area of square
double area = (side * side);
Console.Write(" Square [ side : " + side + " ] ");
Console.Write("\n Area : " + area + "\n\n");
}
public static void Main(String[] args)
{
SquareSize obj = new SquareSize();
//Test Cases
obj.square_area(5);
obj.square_area(8);
obj.square_area(7.5);
}
}

#### Output

Square [ side : 5 ]
Area : 25

Square [ side : 8 ]
Area : 64

Square [ side : 7.5 ]
Area : 56.25
<?php
// Php Program
// Calculate Area of Square
class SquareSize
{
///Calculate area of square by given side
public 	function square_area(\$side)
{
//Calculate area of square
\$area = (\$side *\$side);
//Display the result of square area
echo(" Square [ side : ". \$side ." ] ");
echo("\n Area : ". \$area ."\n\n");
}
}

function main()
{
\$obj = new SquareSize();
//Test Cases
\$obj->square_area(5);
\$obj->square_area(8);
\$obj->square_area(7.5);
}
main();

#### Output

Square [ side : 5 ]
Area : 25

Square [ side : 8 ]
Area : 64

Square [ side : 7.5 ]
Area : 56.25
// Node Js Program
// Calculate Area of Square
class SquareSize
{
///Calculate area of square by given side
square_area(side)
{
//Calculate area of square
var area = (side *side);
//Display the result of square area
process.stdout.write(" Square [ side : " + side + " ] ");
process.stdout.write("\n Area : " + area + "\n\n");
}
}

function main(args)
{
var obj = new SquareSize();
//Test Cases
obj.square_area(5);
obj.square_area(8);
obj.square_area(7.5);
}
main();

#### Output

Square [ side : 5 ]
Area : 25

Square [ side : 8 ]
Area : 64

Square [ side : 7.5 ]
Area : 56.25
#  Python 3 Program
#  Calculate Area of Square
class SquareSize :
# /Calculate area of square by given side
def square_area(self, side) :
# Calculate area of square
area = (side * side)
# Display the result of square area
print(" Square [ side : ", side ," ] ", end = "")
print("\n Area : ", area ,"\n\n", end = "")

def main() :
obj = SquareSize()
# Test Cases
obj.square_area(5)
obj.square_area(8)
obj.square_area(7.5)

if __name__ == "__main__": main()

#### Output

Square [ side :  5  ]
Area :  25

Square [ side :  8  ]
Area :  64

Square [ side :  7.5  ]
Area :  56.25
#  Ruby Program
#  Calculate Area of Square
class SquareSize

# Calculate area of square by given side
def square_area(side)

# Calculate area of square
area = (side * side)
# Display the result of square area
print(" Square [ side  :", side ," ] ")
print("\n Area  :", area ,"\n\n")
end
end
def main()

obj = SquareSize.new()
# Test Cases
obj.square_area(5)
obj.square_area(8)
obj.square_area(7.5)
end
main()

#### Output

Square [ side  :5 ]
Area  :25

Square [ side  :8 ]
Area  :64

Square [ side  :7.5 ]
Area  :56.25

// Scala Program
// Calculate Area of Square
class SquareSize
{
//Calculate area of square by given side
def square_area(side: Double): Unit = {
//Calculate area of square
var area: Double = (side * side);
//Display the result of square area
print(" Square [ side : " + side + " ] ");
print("\n Area : " + area + "\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: SquareSize = new SquareSize();
//Test Cases
obj.square_area(5);
obj.square_area(8);
obj.square_area(7.5);
}
}

#### Output

Square [ side : 5.0 ]
Area : 25.0

Square [ side : 8.0 ]
Area : 64.0

Square [ side : 7.5 ]
Area : 56.25
// Swift Program
// Calculate Area of Square
class SquareSize
{
//Calculate area of square by given side
func square_area(_ side: Double)
{
//Calculate area of square
let area: Double = (side * side);
//Display the result of square area
print(" Square [ side : ", side ," ] ", terminator: "");
print("\n Area : ", area ,"\n\n", terminator: "");
}
}
func main()
{
let obj: SquareSize = SquareSize();
//Test Cases
obj.square_area(5);
obj.square_area(8);
obj.square_area(7.5);
}
main();

#### Output

Square [ side :  5.0  ]
Area :  25.0

Square [ side :  8.0  ]
Area :  64.0

Square [ side :  7.5  ]
Area :  56.25

## Output Explanation

The provided code runs three test cases and calculates the areas of squares with different side lengths. The output includes the square's side length 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.

Categories
Relative Post