Puzzle | Program to find number of squares in a chessboard
Last Updated :
27 Mar, 2025
You are provided with a chessboard and are asked to find the total number of squares in it. A chessboard is a board with 8 x 8 grids in it, as represented below.

Examples:
Input: n = 2
Output: 5 (4 squares of 1 unit + 1 square of 2 units)
Explanation: Total number of square = 5 (4 squares with side of 1 unit + 1 square with side of 2 units)
Input: n = 3
Output: 14
Explanation: Total number of square = 14 ( 9 squares with side of 1 unit , 4 square with side of 2 units, 1 square with side of 3 units )
Looking closely at the chessboard we can see that in addition to the 1 x 1 square, there can be a combination of 2 x 2, 3 x 3, 4 x 4, 5 x 5, 6 x 6, 7 x 7, and 8 x 8 squares too. To get the total number of squares we need to find all the squares formed.
No of 1 x 1 Square : 8 * 8 = 64 squares.
No of 2 x 2 Square : 7 * 7 = 49 squares.
No of 3 x 3 Square : 6 * 6 = 36 squares.
No of 4 x 4 Square : 5 * 5 = 25 squares.
No of 5 x 5 Square : 4 * 4 = 16 squares.
No of 6 x 6 Square : 3 * 3 = 9 squares.
No of 7 x 7 Square : 2 * 2 = 4 squares.
No of 8 x 8 Square : 1 * 1 = 1 square.
Therefore, we have in all = 64 + 49 + 36 + 25 + 16 + 9 + 4 + 1 = 204 squares in a chessboard.
By using the above observation the total number of squares formed in a grid of size n*n are:
1^2 + 2^2 + 3^2 + ... + n^2 = n(n+1)(2n+1) / 6
Since the value of n*(n+1)*(2n+1) can cause overflow for large values of n, below are some interesting tricks used in the program.
- long int is used in return.
- n * (n + 1) / 2 is evaluated first as the value n*(n+1) will always be a multiple of 2.
C++
#include <bits/stdc++.h>
using namespace std;
long long int countSquares(int n)
{
return (n * (n + 1) / 2) * (2 * n + 1) / 3;
}
int main()
{
int n = 4;
cout << countSquares(n);
return 0;
}
Java
class GfG
{
static int countSquares(int n)
{
return (n * (n + 1) / 2) * (2 * n + 1) / 3;
}
public static void main (String[] args)
{
int n = 3;
System.out.println(countSquares(n));
}
}
Python
def countSquares(n):
# better way to write
# n*(n+1)*(2n+1)/6
return ((n * (n + 1) / 2)
* (2 * n + 1) / 3)
n = 4
print(countSquares(n))
C#
using System;
public class GFG {
static int countSquares(int n)
{
// A better way to write
// n*(n+1)*(2n+1)/6
return (n * (n + 1) / 2)
* (2 * n + 1) / 3;
}
public static void Main ()
{
int n = 4;
Console.WriteLine(countSquares(n));
}
}
JavaScript
function countSquares( n)
{
// A better way to write n*(n+1)*(2n+1)/6
return (n * (n + 1) / 2) * (2*n + 1) / 3;
}
let n = 4;
console.log(countSquares(n));
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Puzzle | Maximum number of Kings on Chessboard without under check Prerequisite - The Pigeonhole PrincipleProblem statement - Given a 8Ã8 chessboard, figure out the maximum number of kings that can be placed on the chessboard so that no two kings attack each other, i.e., none of the kings is under check. A king can move only one step at a time any direction on the
3 min read
Program to count number of distinct Squares and Cubes upto N Given a number N, the task is to find No. of perfect Squares and perfect Cubes from 1 to a given integer, N ( Both Inclusive).Note: Numbers which are both perfect Square and perfect Cube should be counted once.Examples: Input: N = 70 Output: 10 Explanation: Numbers that are perfect Square or perfect
8 min read
Number of ways to place two queens on a N*N chess-board Given an integer N denoting a N * N chess-board, the task is to count the number of ways to place two queens on the board such that, they do not attack each other. Examples: Input: N = 9 Output: 2184 Explanation: There are 2184 ways to place two queens on 9 * 9 chess-board. Input: N = 3 Output: 8 Ex
5 min read
Number of ways to place two queens on a N*N chess-board Given an integer N denoting a N * N chess-board, the task is to count the number of ways to place two queens on the board such that, they do not attack each other. Examples: Input: N = 9 Output: 2184 Explanation: There are 2184 ways to place two queens on 9 * 9 chess-board. Input: N = 3 Output: 8 Ex
5 min read
Number of squares in a rectangle Given a m x n matrix, count the number of squares in the matrix.Examples : Input: m = 2, n = 2Output: 5Explanation: There are 4 squares of size 1x1 + 1 square of size 2x2.Input: m = 4, n = 3Output: 20Explanation: There are 12 squares of size 1x1 + 6 squares of size 2x2 + 2 squares of size 3x3.[Naive
7 min read
Count squares with odd side length in Chessboard Given a N * N chessboard, the task is to count the number of squares having the odd side length.Example: Input: N = 3 Output: 10 9 squares are possible whose sides are 1 and a single square with side = 3 9 + 1 = 10Input: N = 8 Output: 120 Approach: For all odd numbers from 1 to N and then calculate
4 min read