# 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 = 10

Input:N = 8

Output:120

**Approach:** For all odd numbers from **1** to **N** and then calculate the number of squares that can be formed having that odd side. For the **i ^{th}** side, the count of squares is equal to

**(N – i + 1)**. Further add all such count of squares.

^{2}Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count ` `// of odd length squares possible ` `int` `count_square(` `int` `n) ` `{ ` ` ` ` ` `// To store the required count ` ` ` `int` `count = 0; ` ` ` ` ` `// For all odd values of i ` ` ` `for` `(` `int` `i = 1; i <= n; i = i + 2) { ` ` ` ` ` `// Add the count of possible ` ` ` `// squares of length i ` ` ` `int` `k = n - i + 1; ` ` ` `count += (k * k); ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 8; ` ` ` ` ` `cout << count_square(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to return the count ` ` ` `// of odd length squares possible ` ` ` `static` `int` `count_square(` `int` `n) ` ` ` `{ ` ` ` ` ` `// To store the required count ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// For all odd values of i ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i = i + ` `2` `) { ` ` ` ` ` `// Add the count of possible ` ` ` `// squares of length i ` ` ` `int` `k = n - i + ` `1` `; ` ` ` `count += (k * k); ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `N = ` `8` `; ` ` ` ` ` `System.out.println(count_square(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

` ` `# Python implementation of the approach ` ` ` `# Function to return the count ` `# of odd length squares possible ` `def` `count_square(n): ` ` ` ` ` `# To store the required count ` ` ` `count ` `=` `0` `; ` ` ` ` ` `# For all odd values of i ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `, ` `2` `): ` ` ` ` ` `# Add the count of possible ` ` ` `# squares of length i ` ` ` `k ` `=` `n ` `-` `i ` `+` `1` `; ` ` ` `count ` `+` `=` `(k ` `*` `k); ` ` ` ` ` `# Return the required count ` ` ` `return` `count; ` ` ` `# Driver code ` `N ` `=` `8` `; ` `print` `(count_square(N)); ` ` ` `# This code has been contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return the count ` ` ` `// of odd length squares possible ` ` ` `static` `int` `count_square(` `int` `n) ` ` ` `{ ` ` ` ` ` `// To store the required count ` ` ` `int` `count = 0; ` ` ` ` ` `// For all odd values of i ` ` ` `for` `(` `int` `i = 1; i <= n; i = i + 2) { ` ` ` ` ` `// Add the count of possible ` ` ` `// squares of length i ` ` ` `int` `k = n - i + 1; ` ` ` `count += (k * k); ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 8; ` ` ` ` ` `Console.WriteLine(count_square(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the count ` `// of odd length squares possible ` `function` `count_square(` `$n` `) ` `{ ` ` ` ` ` `// To store the required count ` ` ` `$count` `= 0; ` ` ` ` ` `// For all odd values of i ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `= ` `$i` `+ 2) ` ` ` `{ ` ` ` ` ` `// Add the count of possible ` ` ` `// squares of length i ` ` ` `$k` `=` `$n` `- ` `$i` `+ 1; ` ` ` `$count` `+= (` `$k` `* ` `$k` `); ` ` ` `} ` ` ` ` ` `// Return the required count ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$N` `= 8; ` ` ` `echo` `count_square(` `$N` `); ` ` ` `// This code is contributed by AnkitRai01 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

120

## Recommended Posts:

- Number of squares of side length required to cover an N*M rectangle
- Program to find number of squares in a chessboard
- Find the side of the squares which are inclined diagonally and lined in a row
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Minimum and maximum possible length of the third side of a triangle
- Count Distinct Rectangles in N*N Chessboard
- Area of a n-sided regular polygon with given side length
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N)
- Count Magic squares in a grid
- Count number of squares in a rectangle
- Count number less than N which are product of perfect squares
- Program to count number of distinct Squares and Cubes upto N
- Count the total number of squares that can be visited by Bishop in one move
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Count unique subsequences of length K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.