Area of a leaf inside a square
Last Updated :
02 Jul, 2022
Given an integer a as the side of the square ABCD. The task is to find the area of the leaf AECFA inside the square as shown below:
Examples:
Input: a = 7
Output: 28
Input: a = 21
Output: 252
Approach: To calculate the area of the leaf, first find the area of the half leaf AECA, which can be given as:
Area of half leaf = Area of quadrant AECDA – Area of right triangle ACD.
Thus, Area of half leaf = ( PI * a * a / 4 ) – a * a / 2 where PI = 22 / 7 and a is the side of the square.
Hence, the area of full leaf will be ( PI * a * a / 2 ) – a * a
On taking a * a common we get, Area of leaf = a * a ( PI / 2 – 1 )
Below is the implementation of the above approach:
C
#include <stdio.h>
#define PI 3.14159265
float area_leaf( float a)
{
return (a * a * (PI / 2 - 1));
}
int main()
{
float a = 7;
printf ( "%f" ,
area_leaf(a));
return 0;
}
|
Java
import java.lang.*;
class GFG {
static double PI = 3.14159265 ;
public static double area_leaf( double a)
{
return (a * a * (PI / 2 - 1 ));
}
public static void main(String[] args)
{
double a = 7 ;
System.out.println(area_leaf(a));
}
}
|
Python3
PI = 3.14159265
def area_leaf( a ):
return ( a * a * ( PI / 2 - 1 ) )
a = 7
print (area_leaf( a ))
|
C#
using System;
class GFG {
static double PI = 3.14159265;
public static double area_leaf( double a)
{
return (a * a * (PI / 2 - 1));
}
public static void Main()
{
double a = 7;
Console.Write(area_leaf(a));
}
}
|
PHP
<?php
$PI = 3.14159265;
function area_leaf( $a )
{
global $PI ;
return ( $a * $a * ( $PI / 2 - 1 ) );
}
$a = 7;
echo (area_leaf( $a ));
?>
|
Javascript
<script>
const PI = 3.14159265;
function area_leaf(a)
{
return (a * a * (PI / 2 - 1));
}
let a = 7;
document.write(Math.round(area_leaf(a)));
</script>
|
Time complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...