Thursday, October 9, 2025
HomeData Modelling & AIArea of a Square | Using Side, Diagonal and Perimeter

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 
 

Finding Area of Square using its Side

The area of the square of the side S is given by:

Area = Side * Side

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <iostream>
using namespace std;
 
// Function to find the area of a square
int areaOfSquare(int S)
{
    // Use above formula
    int area = S * S;
 
    return area;
}
 
// Driver Code
int main()
{
 
    // Given Side of square
    int S = 5;
 
    // Function call
    cout << areaOfSquare(S);
    return 0;
}


Java




// Java program for the above approach
class GFG{
  
// Function to find the area of a square
static int areaOfSquare(int S)
{
    // Use above formula
    int area = S * S;
  
    return area;
}
  
// Driver Code
public static void main(String[] args)
{
  
    // Given Side of square
    int S = 5;
  
    // Function call
    System.out.println(areaOfSquare(S));
}
}
 
// This code is contributed by rock_cool


Python3




# Python3 program for the above approach
 
# Function to find the area of a square
def areaOfSquare(S):
 
    # Use above formula
    area = S * S
 
    return area
 
# Driver Code
if __name__ == '__main__':
 
    # Given Side of square
    S = 5
 
    # Function call
    print(areaOfSquare(S))
 
# This code is contributed by Mohit Kumar


C#




// C# program for the above approach
using System;
class GFG{
   
// Function to find the area of a square
static int areaOfSquare(int S)
{
    // Use above formula
    int area = S * S;
   
    return area;
}
   
// Driver Code
public static void Main(string[] args)
{
   
    // Given Side of square
    int S = 5;
   
    // Function call
    Console.Write(areaOfSquare(S));
}
}
 
// This code is contributed by Ritik Bansal


Javascript




<script>
 
    // Javascript program for the above approach
     
    // Function to find the area of a square
    function areaOfSquare(S)
    {
        // Use above formula
        let area = S * S;
 
        return area;
    }
 
    // Given Side of square
    let S = 5;
  
    // Function call
    document.write(areaOfSquare(S));
     
    // This code is contributed by divyeshrabadiya07.
     
</script>


Output: 

25

 

Time Complexity: O(1)
Auxiliary Space: O(1)

Finding Area of Square using its Diagonal

  • 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:

D = \sqrt{2}*S
 

   …(2) 
 

 

  • Substituting the value of S from Equation (2) in Equation (1), we have:
     

Area = (\frac{D}{\sqrt{2}})^{2} = \frac{D*D}{2}

Below is the implementation of the above approach:

 

C++




// C++ program for the above approach
#include <iostream>
using namespace std;
 
// Function to find the area of a square
int areaOfSquare(int D)
{
    // Use above formula
    int area = (D * D) / 2;
 
    return area;
}
 
// Driver Code
int main()
{
 
    // Given diagonal of square
    int D = 4;
 
    // Function call
    cout << areaOfSquare(D);
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the area of a square
static int areaOfSquare(int D)
{
     
    // Use above formula
    int area = (D * D) / 2;
 
    return area;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given diagonal of square
    int D = 4;
 
    // Function call
    System.out.print(areaOfSquare(D));
}
}
 
// This code is contributed by PrinciRaj1992


Python3




# Python3 program for the above approach
 
# Function to find the area of a square
def areaOfSquare(D):
 
    # Use above formula
    area = (D * D) // 2;
 
    return area;
 
# Driver Code
if __name__ == '__main__':
 
    # Given diagonal of square
    D = 4;
    # Function call
    print(areaOfSquare(D));
 
# This code is contributed by PrinciRaj1992


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the area of a square
static int areaOfSquare(int D)
{
     
    // Use above formula
    int area = (D * D) / 2;
 
    return area;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given diagonal of square
    int D = 4;
 
    // Function call
    Console.Write(areaOfSquare(D));
}
}
 
// This code is contributed by amal kumar choubey


Javascript




<script>
    // Javascript program for the above approach
     
    // Function to find the area of a square
    function areaOfSquare(D)
    {
        // Use above formula
           let area = parseInt((D * D) / 2, 10);
 
        return area;
    }
     
    // Given diagonal of square
    let D = 4;
  
    // Function call
    document.write(areaOfSquare(D));
     
</script>


Output: 

8

 

Time Complexity: O(1)
Auxiliary Space: O(1)

Finding Area of Square using its Perimeter

  • 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:

Area = (\frac{P}{4})^{2} = \frac{P*P}{4}

Below is the implementation of the above formula:

C++




// C++ program for the above approach
#include <iostream>
using namespace std;
 
// Function to find the area of a square
int areaOfSquare(int P)
{
    // Use above formula
    int area = (P * P) / 16;
 
    return area;
}
 
// Driver Code
int main()
{
    // Given perimeter of square
    int P = 32;
 
    // Function call
    cout << areaOfSquare(P);
    return 0;
}


Java




// Java program for the above approach
class GFG{
 
// Function to find the area of a square
static int areaOfSquare(int P)
{
     
    // Use above formula
    int area = (P * P) / 16;
 
    return area;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given perimeter of square
    int P = 32;
 
    // Function call
    System.out.print(areaOfSquare(P));
}
}
 
// This code is contributed by amal kumar choubey


Python3




# Python3 program for the above approach
 
# Function to find the area of a square
def areaOfSquare(P):
   
    # Use above formula
    area = (P * P) // 16;
 
    return area;
 
# Driver Code
if __name__ == '__main__':
   
    # Given perimeter of square
    P = 32;
 
    # Function call
    print(areaOfSquare(P));
 
# This code is contributed by gauravrajput1


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the area of a square
static int areaOfSquare(int P)
{
     
    // Use above formula
    int area = (P * P) / 16;
 
    return area;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given perimeter of square
    int P = 32;
 
    // Function call
    Console.Write(areaOfSquare(P));
}
}
 
// This code is contributed by amal kumar choubey


Javascript




<script>
 
// javascript program for the above approach
// Function to find the area of a square
function areaOfSquare(P)
{
     
    // Use above formula
    var area = (P * P) / 16;
 
    return area;
}
 
// Driver Code
//Given perimeter of square
var P = 32;
 
// Function call
document.write(areaOfSquare(P));
 
// This code is contributed by Princi Singh
</script>


Output: 

64

 

Time Complexity: O(1)
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

Dominic
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11876 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS