C++ Program for Area Of Square after N-th fold

C++Server Side ProgrammingProgramming

Given a side of a square and the number of fold, we have to find the Area of square after number of folds.

A square is a 2-D shape like rectangle where all the sides are equal. And it’s all angles are equal to 90 degrees.

While folding a square we −

  • Fold the square from top left side of the triangle to the bottom of the right side forming a triangle.

  • The second fold will be folding from up to downside.

  • The third fold is folding again from left to right.

And likewise we follow the above steps.

Examples

Input: side = 23, fold = 4
Output: area of square after n folds is : 6.53086

To solve this problem we can follow the below approach −

  • Firstly, we have to find the area of a square before folding the square.
  • With every fold we have to half the area of the square Area = Area/2.
  • Finally we will divide the area of square by pow(2, fold)

Algorithm

START
   In function double area_nfold(double side, double fold)
   Step 1-> Decalre and initialize area = side * side
   Step 2-> Return (area * 1.0 / pow(3, fold))
   In int main()
   Step 1 -> Decalre and initialize double side = 23, fold = 4
   Step 2 -> Call function area_nfold(side, fold) and print the results
STOP

Example

#include <bits/stdc++.h>
using namespace std;
//function to calculate area of square after n folds
double area_nfold(double side, double fold){
   double area = side * side;
   return area * 1.0 / pow(3, fold);
}
int main(){
   double side = 23, fold = 4;
   cout <<"area of square after n folds is :"<<area_nfold(side, fold);
   return 0;
}

Output

area of square after n folds is :6.53086
raja
Published on 23-Sep-2019 15:19:01
Advertisements