Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmArea of a square from diagonal length

Area of a square from diagonal length

Given an number d which is length of diagonal of a square, find its area. 
Examples: 
 

Input : d = 10
Output : Area = 50

Input : d = 12.2
Output : Area = 74.42

 

Area of a square can be computed as (d * d)/2. Please see below image for details.
 

 

C++




// C++ Program to find the area of square
// when its diagonal is given.
#include <bits/stdc++.h>
using namespace std;
  
// Returns area of square from given
// diagonal
double findArea(double d)
{
    return (d * d) / 2.0;
}
  
// Driver Code
int main()
{
    double d = 10;
    cout << (findArea(d));
    return 0;
}
  
// This code is contributed by
// Shivi_Aggarwal


C




// C Program to find the area of square
// when its diagonal is given.
#include <stdio.h>
  
// Returns area of square from given
// diagonal
double findArea(double d)
{
    return (d * d) / 2;
}
  
// Driver function.
int main()
{
    double d = 10;
    printf("%.2f", findArea(d));
    return 0;
}


Java




// Java Program to find the area of square
// when its diagonal is given.
  
class GFG 
{
    // Returns area of square from given
    // diagonal
    static double findArea(double d)
    {
        return (d * d) / 2;
    }
      
    // Driver code
    public static void main (String[] args) 
    {
        double d = 10;
        System.out.println(findArea(d));
    }
}
  
// This code is contributed by Anant Agarwal.


Python3




# Python3 Program to find
# the area of square
# when its diagonal is given.
  
# Returns area of square from given
# diagonal
def findArea(d):
  
    return (d * d) / 2
  
# Driver function.
d = 10
print("%.2f" % findArea(d))
  
# This code is contributed by
# Smitha Dinesh Semwal


C#




// C# Program to find the area of square
// when its diagonal is given.
using System;
  
class GFG 
{
    // Returns area of square from given
    // diagonal
    static double findArea(double d)
    {
        return (d * d) / 2;
    }
      
    // Driver code
    public static void Main () 
    {
        double d = 10;
        Console.WriteLine(findArea(d));
    }
}
  
// This code is contributed by vt_m.


PHP




<?php
// PHP Program to find the area of 
// square when its diagonal is given.
  
// Returns area of square 
// from given diagonal
function findArea( $d)
{
    return ($d * $d) / 2;
}
  
    // Driver Code
    $d = 10;
    echo( findArea($d));
      
// This code is contributed by vt_m.
?>


Javascript




<script>
  
// JavaScript Program to find the area of square
// when its diagonal is given.
  
 // Returns area of square from given
    // diagonal
    function findArea(d)
    {
        return (d * d) / 2;
    }
   
// Driver code
  
       let d = 10;
       document.write(findArea(d));
        
</script>


Output: 

50.00

 

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!

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments