C++ Program to print a chessboard pattern

This C++ program will print a chessboard like pattern using loops.
Submitted by Abhishek Pathak, on April 09, 2017 [Last updated : February 27, 2023]

Printing a chessboard pattern

You must have played chess in your once in your life, so why not create a pattern that resembles to it? Like most of the pattern based programs, this program is simply a code that prints a square chessboard up to N x N size. Here is an output for what we want to print.

# # # # 
 # # # #
# # # # 
 # # # #
# # # # 
 # # # #
# # # # 
 # # # #

C++ code to print a chessboard pattern

#include<iostream>

using namespace std;

int main() {
	int size = 8;
	
	for(int i=0; i<size; i++) {
		for(int j=0; j<size; j++) {
			if((i+j)%2 == 0)
				cout<<"#";
			else
				cout<<" ";
		}
		cout<<"\n";
	}
	return 0;
}

Output

# # # # 
 # # # #
# # # # 
 # # # #
# # # # 
 # # # #
# # # # 
 # # # #

So, in this code, we are running two loops, one to control rows and another to columns. Then we are simply checking the alteration using the positions. When both i+j are even, then we substitute # otherwise we print a space character.

Easy it was. Right?



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.