Area of a square from diagonal length
Last Updated :
20 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
double findArea( double d)
{
return (d * d) / 2.0;
}
int main()
{
double d = 10;
cout << (findArea(d));
return 0;
}
|
C
#include <stdio.h>
double findArea( double d)
{
return (d * d) / 2;
}
int main()
{
double d = 10;
printf ( "%.2f" , findArea(d));
return 0;
}
|
Java
class GFG
{
static double findArea( double d)
{
return (d * d) / 2 ;
}
public static void main (String[] args)
{
double d = 10 ;
System.out.println(findArea(d));
}
}
|
Python3
def findArea(d):
return (d * d) / 2
d = 10
print ( "%.2f" % findArea(d))
|
C#
using System;
class GFG
{
static double findArea( double d)
{
return (d * d) / 2;
}
public static void Main ()
{
double d = 10;
Console.WriteLine(findArea(d));
}
}
|
PHP
<?php
function findArea( $d )
{
return ( $d * $d ) / 2;
}
$d = 10;
echo ( findArea( $d ));
?>
|
Javascript
<script>
function findArea(d)
{
return (d * d) / 2;
}
let d = 10;
document.write(findArea(d));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...