Java Program to Find Maximum Number of Squares That Can Fit in a Right-Angle Isosceles Triangle

In the previous article, we have seen Java Program to Find Minimum Height of the Triangle with Given Base and Area

In this article we will discuss about how to find maximum number of squares that can fit in a right-angle Isosceles Triangle using Java programming language.

Java Program to Find Maximum Number of Squares That Can Fit in a Right-Angle Isosceles Triangle

Before jumping into the program directly, let’s first know how can we find maximum number of squares that can fit in a right-angle isosceles triangle.

Explanation:

Let us consider a right angle triangle with height 'h'. 
If we remove squares of 's' size from the triangle’s base, we are left with a new right angle triangle of height (h-s)
f(h,s) = h/s – 1 + f(h-s,s)

Example:

When h=6 and s=2

f(6,2) = 6/2 – 1 + f(6-2,4)

=> 4-1+4/4-1 = 3

Let’s see different ways to find maximum number of squares that can fit in a right-angle Isosceles Triangle.

Method-1: Java Program to Find Maximum Number of Squares That Can Fit in a Right-Angle Isosceles Triangle By Using Static Value

Approach:

  1. Declare the value for height of the triangle and square side.
  2. Then call the maxSquare() method by passing both value as parameter.
  3. In this method the number of squares that can be fit will be calculated by the formula  (h/s-1)*(h/s)/2
  4. Then print the result.

Program:

public class Main
{
    public static void main(String[] args)
    {
        // Static values for the side of the triangle and the side of the square   
        int height = 6, square = 2;
        System.out.println("The number of squares which can be fit into the triangle are: "+maxSquare(height,square));
    }

    // Function that uses recursion to find out the number of squares 
    // which can fit inside the triangle
    static int maxSquare(int h, int s)
    {
        return (h/s-1)*(h/s)/2;
    }
}
Output:

The number of squares which can be fit into the triangle are: 3

Method-2: Java Program to Find Maximum Number of Squares That Can Fit in a Right-Angle Isosceles Triangle By Using User Input Value

Approach:

  1. Take user input for the value for height of the triangle  and square side.
  2. Then call the maxSquare() method by passing both value as parameter.
  3. In this method the number of squares that can be fit will be calculated by the formula  (h/s-1)*(h/s)/2
  4. Then print the result.

Program:

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        // Asking the user for the side of the triangle and the side of the square   
        System.out.println("Enter the side of the triangle and side of the square");
        int height = scan.nextInt(), square = scan.nextInt();
        System.out.println("The number of squares that can be fit into the triangle are - "+maxSquare(height,square));
    }

    // Function that uses recursion to find out the number of squares
    // Which can fit inside the triangle
    static int maxSquare(int h, int s)
    {
        return (h/s-1)*(h/s)/2;
    }
}
Output:

Enter the side of the triangle and side of the square
9 4
The number of squares that can be fit into the triangle are - 1

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Articles: