Given here are **n** squares which touch each other externally, and are lined up in a row. The distance between the centers of the first and last square is given. The squares have equal side length. The task is to find the side of each square.

**Examples:**

Input:d = 42, n = 4Output:The side of each square is 14Input:d = 36, n = 5Output:The side of each square is 9

**Approach:**

Suppose there are n squares each having side of length **a**.

Let, the distance between the first and last squares = **d**

From the figure, it is clear,

**a/2 + a/2 + (n-2)*a = d
a + na – 2a = d
na – a = d**

so,

**a = d/(n-1)**

## C++

`// C++ program to find side of the squares ` `// which are lined in a row and distance between the ` `// centers of first and last squares is given ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `radius(` `int` `n, ` `int` `d) ` `{ ` ` ` `cout << ` `"The side of each square is "` ` ` `<< d / (n - 1) << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `d = 42, n = 4; ` ` ` `radius(n, d); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find side of the squares ` `// which are lined in a row and distance between the ` `// centers of first and last squares is given ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `static` `void` `radius(` `int` `n, ` `int` `d) ` `{ ` ` ` `System.out.print( ` `"The side of each square is "` ` ` `+ d / (n - ` `1` `)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `d = ` `42` `, n = ` `4` `; ` ` ` `radius(n, d); ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## Python3

` ` `# Python program to find side of the squares ` `# which are lined in a row and distance between the ` `# centers of first and last squares is given ` ` ` `def` `radius(n, d): ` ` ` ` ` `print` `(` `"The side of each square is "` `, ` ` ` `d ` `/` `(n ` `-` `1` `)); ` ` ` ` ` `d ` `=` `42` `; n ` `=` `4` `; ` `radius(n, d); ` ` ` ` ` ` ` `# This code contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find side of the squares ` `// which are lined in a row and distance between the ` `// centers of first and last squares is given ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `void` `radius(` `int` `n, ` `int` `d) ` `{ ` ` ` `Console.Write( ` `"The side of each square is "` ` ` `+ d / (n - 1)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `d = 42, n = 4; ` ` ` `radius(n, d); ` `} ` `} ` ` ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

**Output:**

The side of each square is 14

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the radii of the circles which are lined in a row, and distance between the centers of first and last circle is given
- Find the side of the squares which are inclined diagonally and lined in a row
- Distance between centers of two intersecting circles if the radii and common chord length is given
- Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles
- Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles
- Number of common tangents between two circles if their centers and radius is given
- Find side of Square which makes minimal area to fit two identical rectangles inside it
- Area of Triangle using Side-Angle-Side (length of two sides and the included angle)
- Distance of chord from center when distance between center and another equal length chord is given
- Find the number of squares inside the given square grid
- Number of squares of side length required to cover an N*M rectangle
- Count squares with odd side length in Chessboard
- Maximum possible intersection by moving centers of line segments
- Length of the perpendicular bisector of the line joining the centers of two circles
- Program to find the side of the Octagon inscribed within the square
- Find a Square Matrix such that sum of elements in every row and column is K
- Minimum side of square embedded in Regular polygon with N sides
- Find the index which is the last to be reduced to zero after performing a given operation
- Calculate the Square of Euclidean Distance Traveled based on given conditions
- Square pyramidal number (Sum of Squares)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.