Computer >> Computer tutorials >  >> Programming >> C programming

Area of squares formed by joining mid points repeatedly in C Program?


Suppose we have one square whose side is ‘a’. We will make more squares by attaching the mid-points of the squares repeatedly. The number of repetition is n times. We have to find the area of nth square.

Area of squares formed by joining mid points repeatedly in C Program?

As the side of the outer square is ‘a’, then area is

Area of squares formed by joining mid points repeatedly in C Program?

Now using Pythagorean theorem, we can get the area of the second rectangle is −

Area of squares formed by joining mid points repeatedly in C Program?

Similarly, area of 3rd square is −

Area of squares formed by joining mid points repeatedly in C Program?

Using this we can understand that the area of nth square is −

Area of squares formed by joining mid points repeatedly in C Program?

Example

#include <iostream>
#include <cmath>
using namespace std;
float area(float a, float n) {
   if (a < 0 ) //if the value is negative it is invalid
      return -1;
   float area = (a*a) / pow(2, n-1);
   return area;
}
int main() {
   float a = 20.0, n = 10.0;
   cout << "Area : " << area(a, n);
}

Output

Area : 0.78125