Count number of squares in a rectangle | Java program to count squares in a rectangle of size m x n ; Returns count of all squares in a rectangle of size m x n ; If n is smaller , swap m and n ; Now n is greater dimension , apply formula ; Driver Code
import java . util . * ; class GFG { static int countSquares ( int m , int n ) { if ( n < m ) { int temp = m ; m = n ; n = temp ; } return n * ( n + 1 ) * ( 3 * m - n + 1 ) / 6 ; } public static void main ( String [ ] args ) { int m = 4 ; int n = 3 ; System . out . print ( " Count ▁ of ▁ squares ▁ is ▁ " + countSquares ( m , n ) ) ; } }