Area of a Square | Using Side, Diagonal and Perimeter
Given one of the Sides S, Diagonal D, or Perimeter P of the square, the task is to find the area of the square with the given value.
Examples:
Input: S = 5
Output: Area of the square using side = 25
Input: D = 4
Output: Area of the square using diagonal = 8
Input: P = 32
Output: Area of the square using perimeter = 64
The area of the square of the side S is given by:
Area = Side * Side
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int areaOfSquare( int S)
{
int area = S * S;
return area;
}
int main()
{
int S = 5;
cout << areaOfSquare(S);
return 0;
}
|
Java
class GFG{
static int areaOfSquare( int S)
{
int area = S * S;
return area;
}
public static void main(String[] args)
{
int S = 5 ;
System.out.println(areaOfSquare(S));
}
}
|
Python3
def areaOfSquare(S):
area = S * S
return area
if __name__ = = '__main__' :
S = 5
print (areaOfSquare(S))
|
C#
using System;
class GFG{
static int areaOfSquare( int S)
{
int area = S * S;
return area;
}
public static void Main( string [] args)
{
int S = 5;
Console.Write(areaOfSquare(S));
}
}
|
Javascript
<script>
function areaOfSquare(S)
{
let area = S * S;
return area;
}
let S = 5;
document.write(areaOfSquare(S));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
- The Area of the square of given side S is given by:
Area = S * S …(1)
- The relation between Side S and Diagonal D is given by:
…(2)
- Substituting the value of S from Equation (2) in Equation (1), we have:
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int areaOfSquare( int D)
{
int area = (D * D) / 2;
return area;
}
int main()
{
int D = 4;
cout << areaOfSquare(D);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int areaOfSquare( int D)
{
int area = (D * D) / 2 ;
return area;
}
public static void main(String[] args)
{
int D = 4 ;
System.out.print(areaOfSquare(D));
}
}
|
Python3
def areaOfSquare(D):
area = (D * D) / / 2 ;
return area;
if __name__ = = '__main__' :
D = 4 ;
print (areaOfSquare(D));
|
C#
using System;
class GFG{
static int areaOfSquare( int D)
{
int area = (D * D) / 2;
return area;
}
public static void Main(String[] args)
{
int D = 4;
Console.Write(areaOfSquare(D));
}
}
|
Javascript
<script>
function areaOfSquare(D)
{
let area = parseInt((D * D) / 2, 10);
return area;
}
let D = 4;
document.write(areaOfSquare(D));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
- The Area of the square of given side S is given by:
Area = S * S …(1)
- The relation between Side S and Perimeter P is given by:
P =4*S …(2)
- Substituting the value of S from Equation (2) in Equation (1), we have:
Below is the implementation of the above formula:
C++
#include <iostream>
using namespace std;
int areaOfSquare( int P)
{
int area = (P * P) / 16;
return area;
}
int main()
{
int P = 32;
cout << areaOfSquare(P);
return 0;
}
|
Java
class GFG{
static int areaOfSquare( int P)
{
int area = (P * P) / 16 ;
return area;
}
public static void main(String[] args)
{
int P = 32 ;
System.out.print(areaOfSquare(P));
}
}
|
Python3
def areaOfSquare(P):
area = (P * P) / / 16 ;
return area;
if __name__ = = '__main__' :
P = 32 ;
print (areaOfSquare(P));
|
C#
using System;
class GFG{
static int areaOfSquare( int P)
{
int area = (P * P) / 16;
return area;
}
public static void Main(String[] args)
{
int P = 32;
Console.Write(areaOfSquare(P));
}
}
|
Javascript
<script>
function areaOfSquare(P)
{
var area = (P * P) / 16;
return area;
}
var P = 32;
document.write(areaOfSquare(P));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
28 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...