Suppose we have a square with side length "a". We will make more squares by repeatedly connecting the midpoints of the squares. The number of repetitions is n times. We have to find the area of the nth square.
Since the side length of the outer square is "a", the area is
Now use Pythagoras According to Sri Lanka's theorem, we can get that the area of the second rectangle is -
Similarly, the area of the third rd square is -
Using this we can know that the area of the nth square is-
#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); }
Area : 0.78125
The above is the detailed content of What is the area of the square formed by repeatedly connecting midpoints in a C program?. For more information, please follow other related articles on the PHP Chinese website!