# Java Program to Find the Side of the Squares which are Inclined Diagonally and Lined in a Row

In the previous article, we have discussed about Java Program to Find Length of the Chord of the Circle whose Radius and the Angle Subtended at the Center by the Chord is Given

In this article we are going to see how to find the side of the squares which are inclined diagonally and lined in a row by using Java programming language.

## Java Program to Find the Side of the Squares which are Inclined Diagonally and Lined in a Row

Before Jumping into the program directly let’s see how to find the side of the squares which are inclined diagonally and lined in a row.

Explanation:

The distance between the centers of the first and last square is given as PQ = d (given)

Let no. Of squares = n (given)

All the squares are touching each-other’s vertices externally.

All the squares have equal sides = a

Now, we need to find the side of each square. From the figure, we can see that

All the squares are connected by diagonals.

Length of each diagonal = a√2.
For the first and last square, diagonal is covered under the length d = a√2/2 = a/√2

For rest (n-2) squares, diagonal is covered in d = (n-2)*a√2

Hence the relation between a and d is given as follows:

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

Example:

n = 6

d = 25

a =  d/((n-1)*(√2)) = 3.535533

Let’s see different ways to find the side of the squares which are inclined diagonally and lined in a row.

### Method-1: Java Program to Find the Side of the Squares which are Inclined Diagonally and Lined in a Row By Using Static Input Value

Approach:

• Declare an int variable say ‘n’ and assign the value to it, which holds the number of squares.
• Declare a double variable say ‘d’ and assign the value to it, which holds the distance between the centers of first and last square.
• Find the sides of each square using the formula d/((n-1)*(√2))
• Print the result.

Program:

import java.io.*;
class Main
{
public static void main(String [] args)
{
int n = 6;
double d = 25;
// formula to find length of each square
double a =  d/((n-1)*(Math.sqrt(2)));
System.out.println("The length of each square is " + a);
}
}

Output:

The length of each square is 3.5355339059327373

### Method-2: Java Program to Find the Side of the Squares which are Inclined Diagonally and Lined in a Row By Using User Input Value

Approach:

• Declare an int variable say ‘n’ which holds the number of squares.
• Declare a double variable say ‘d’ which holds the distance between the centers of first and last square.
• Then we will take the value of “n”, “d” as user input using scanner class.
• Find the sides of each square using the formula d/((n-1)*(√2))
• Print the result.

Program:

import java.io.*;
import java.util.Scanner;
class Main
{
public static void main(String [] args)
{
// scanner class obj ref
Scanner s = new Scanner(System.in);
System.out.println("Enter the no. Of squares");
// to take user input value
int n = s.nextInt();
System.out.println("Enter the distance between the centers of 1st and last square");
double d = s.nextDouble();
// formula to find length of each square
double a =  d/((n-1)*(Math.sqrt(2)));
System.out.println("The length of each square is " + a);
}
}

Output:

Enter the no. Of squares
10
Enter the distance between the centers of 1st and last square
200
The length of each square is 15.713484026367722

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: