- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Maximum number of squares that can fit in a right angle isosceles triangle in C++

Given the task is to find the maximum number of squares having side ‘a’ that can fit inside a give right angle isosceles triangle with base of ‘s’(An isosceles triangle has at least 2 equal sides).

Let’s now understand what we have to do using an example:

## Input

s=5, a=1

## Output

10

**Explanation** − The number of squares in the base can be calculated by dividing s by a and subtracting 1. So the number of squares in base = 5/1 – 1 = 4.

Similarly when the bottom 4 squares are placed then we get a new isosceles triangle with base (s-a) and then we repeat the same procedure and get 3 squares and so on until a single square is placed at the top.

## Input

s=7, a=2

## Output

3

## Approach used in the below program as follows

To find the maximum number of squares we will have to start from the base of the triangle and find out the number of squares.

To find the number of squares we will divide the base s by the side of the square a and subtract 1 from it = s/a – 1.

Then it will leave us another isosceles triangle with base (s - a) which will accommodate one less square than the previous row below it which we can calculate in the following way −

Squares in next row = (s - a)/a – 1 = (s/a – a/a) – 1= s/a - 1 - 1 = s/a – 2 = one square less than the previous row.

The number of squares keep decreasing until it reaches 1, therefore we have to only find the number of squares in the base row and use the formula of adding natural numbers to find the final sum which is −

(n) * (n + 1) / 2

In this case the formula becomes − ((s / a) – 1) * (s / a) / 2

## Example

#include<bits/stdc++.h> using namespace std; int Max(int s, int a){ //formula for calculating maximum squares return ((s / a) - 1) * (s / a) / 2; } //Main function int main(){ int s = 5, a = 1; cout <<"Maximum squares possible are: "<<Max(s,a); return 0; }

## Output

10

- Related Articles
- Maximum number of 2×2 squares that can be fit inside a right isosceles triangle in C
- Maximum sum of a path in a Right Number Triangle in C++
- Prove that in a right angle triangle, the square of the hypotenuse is equal the sum of squares of the other two sides.
- Determine the measure of each of the equal angles of a right angled isosceles triangle.OR$ABC$ is a right angled triangle in which $\angle A = 90^o$ and $AB = AC$. Find $\angle B$ and $\angle C$.
- Program to find maximum number of boxes we can fit inside another boxes in python
- If the bisector of the exterior vertical angle of a triangle be parallel to the base. Show that the triangle is isosceles.
- The ratio between a base angle and the vertical angle of an isosceles triangle is 1:4. Find each angle of the triangle.
- Find other two sides of a right angle triangle in C++
- ABC is an isosceles triangle with AC = BC. If $AB^2 = 2AC^2$. Prove that ABC is a right triangle.
- \( \triangle \mathrm{ABC} \) is an isosceles triangle in which \( \mathrm{AB}=\mathrm{AC} \). Side \( \mathrm{BA} \) is produced to \( \mathrm{D} \) such that \( \mathrm{AD}=\mathrm{AB} \) (see Fig. 7.34). Show that \( \angle \mathrm{BCD} \) is a right angle."\n
- Show that the bisectors of the base angles of a triangle cannot enclose a right angle in any case.
- Construct an isosceles right-angled triangle $ABC$, where $m\angle ACB = 90^{\circ}$ and $AC = 6\ cm$.
- Maximum Number of Events That Can Be Attended in C++
- Maximum number of candies that can be bought in C
- If one angle of a triangle is equal to the sum of the other two, show that the triangle is a right triangle.