Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Area of squares formed by joining mid points repeatedly in C Program?
In this problem, we create a series of squares by repeatedly connecting the midpoints of the sides of the previous square. Given an initial square with side length 'a' and the number of iterations 'n', we need to find the area of the nth square.
Syntax
float calculateNthSquareArea(float side, int n);
Mathematical Formula
When we connect the midpoints of a square, the resulting inner square has an area that is half of the original square. This pattern continues for each iteration −
- 1st square: Area = a²
- 2nd square: Area = a²/2
- 3rd square: Area = a²/4
- nth square: Area = a²/2^(n-1)
Example
Here's a complete C program to calculate the area of the nth square −
#include <stdio.h>
#include <math.h>
float calculateNthSquareArea(float side, int n) {
if (side < 0 || n <= 0) {
return -1; // Invalid input
}
float area = (side * side) / pow(2, n - 1);
return area;
}
int main() {
float side = 20.0;
int iterations = 10;
printf("Initial square side length: %.2f<br>", side);
printf("Number of iterations: %d<br>", iterations);
float result = calculateNthSquareArea(side, iterations);
if (result == -1) {
printf("Invalid input values!<br>");
} else {
printf("Area of %dth square: %.5f<br>", iterations, result);
}
// Show progression for first few squares
printf("\nProgression:<br>");
for (int i = 1; i <= 5; i++) {
printf("Square %d: Area = %.4f<br>", i, calculateNthSquareArea(side, i));
}
return 0;
}
Initial square side length: 20.00 Number of iterations: 10 Area of 10th square: 0.78125 Progression: Square 1: Area = 400.0000 Square 2: Area = 200.0000 Square 3: Area = 100.0000 Square 4: Area = 50.0000 Square 5: Area = 25.0000
Key Points
- Each new square has half the area of the previous square
- The formula follows a geometric progression with ratio 1/2
- Input validation ensures positive side length and iteration count
- The
pow()function frommath.hcalculates 2^(n-1)
Conclusion
The area of squares formed by connecting midpoints follows the pattern a²/2^(n-1), where each iteration halves the area. This geometric relationship makes the calculation straightforward using the power function.
Advertisements
