- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given a number size as input as dimension of size*size Chessboard. The goal is to find the number of squares that can be formed inside that board having odd lengths.

**For Example**

size=3

Count of squares with odd side length in Chessboard are: 10

All squares will be as shown : and 1 whole square of size 3x3.

size=4

Count of squares with odd side length in Chessboard are: 20

there will be 16, 1X1 squares. And 4, 3X3 squares inside it.

**Approach used in the below program is as follows** −

In this approach we will traverse from length of square as 1 to length as size. For each odd length we will add ( size−i−1)2 to the count.

Take an integer size as input for Chessboard’s side.

Function square_odd_length(int size) takes size and returns count of squares with odd side length in Chessboard.

Take the initial count as 0.

Traverse from i=1 to i=size increment by 2 for odd values of i.

For each i take temp=size−i+1.

Add temp*temp to count.

At the end of the for loop return count as result.

#include <bits/stdc++.h> using namespace std; int square_odd_length(int size){ int count = 0; for (int i = 1; i <= size; i = i + 2){ int temp = size − i + 1; count = count + (temp * temp); } return count; } int main(){ int size = 6; cout<<"Count squares with odd side length in Chessboard are: "<<square_odd_length(size); return 0; }

If we run the above code it will generate the following output −

Count squares with odd side length in Chessboard are: 56

- Related Questions & Answers
- Program to find number of squares in a chessboard in C++
- Count pairs with Odd XOR in C++
- Count Magic squares in a grid in C++
- Area of a n-sided regular polygon with given side length in C++
- Count number of squares in a rectangle in C++
- Count pairs with Bitwise AND as ODD number in C++
- Count pairs with Bitwise XOR as ODD number in C++
- Count subarrays with same even and odd elements in C++
- Transform to Chessboard in C++
- Find Maximum side length of square in a Matrix in C++
- Knight Probability in Chessboard in C++
- Count number of ordered pairs with Even and Odd Product in C++
- Count number of ordered pairs with Even and Odd Sums in C++
- Maximum Side Length of a Square with Sum Less than or Equal to Threshold in C++
- Perfect Squares in C++

Advertisements