Program for Area Of Square after N-th fold
Last Updated :
21 Jun, 2022
A square is a flat shape, in one plane, defined by four points at the four corners. A square has four sides all of equal length, and four corners, all right angles (90 degree angles). A square is a kind of rectangle.
Given a square of side and number of folds . The task is to find the area of square after F-th fold.
The folding of square is as follows:
- In 1st folding, fold the square from left to right side in the form a triangle.
- In 2nd folding, fold the square from up to down side.
- In 3rd folding, again fold the square from left to right side.
Examples:
Input : N = 4, F = 2
Output : 2
Explanation:
Initially square side is 4 x 4
After 1st folding, square side becomes 4 x 2
After 2nd folding, square side becomes 2 x 2
Thus area equals 2 x 2 = 4.
Input : N = 100, F = 6
Output : 156.25
Approach:
- Initially calculate the area of the square before folding.
- After each folding, the area of square reduces by half. That is, area = area/2.
- So, we finally divide the area of square by pow(2, F)
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double areaSquare( double side, double fold)
{
double area = side * side;
return area * 1.0 / pow (2, fold);
}
int main()
{
double side = 4, fold = 2;
cout << areaSquare(side, fold);
return 0;
}
|
Java
class GFG
{
static double areaSquare( double side,
double fold)
{
double area = side * side;
return area * 1.0 / Math.pow( 2 , fold);
}
public static void main(String []args)
{
double side = 4 , fold = 2 ;
System.out.println(areaSquare(side, fold));
}
}
|
Python3
def areaSquare(side, fold) :
area = side * side
ans = area / pow ( 2 , fold)
return ans
if __name__ = = "__main__" :
side = 4
fold = 2
print (areaSquare(side, fold))
|
C#
using System;
class GFG
{
static double areaSquare( double side,
double fold)
{
double area = side * side;
return area * 1.0 / Math.Pow(2, fold);
}
public static void Main()
{
double side = 4, fold = 2;
Console.Write(areaSquare(side, fold));
}
}
|
PHP
<?php
function areaSquare( $side , $fold )
{
$area = $side * $side ;
return $area * 1.0 / pow(2, $fold );
}
$side = 4;
$fold = 2;
echo areaSquare( $side , $fold );
?>
|
Javascript
<script>
function areaSquare( side, fold)
{
var area = side * side;
return (area * 1.0 )/ (Math.pow(2, fold));
}
var side = 4, fold = 2;
document.write(areaSquare(side, fold));
</script>
|
Time complexity : O(logn)
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...