Q:

C++ Program to print a chessboard pattern

0

C++ Program to print 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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

Program to print chessboard pattern in C++

#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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Most popular and Searched C++ solved programs with Explanation and Output

Similar questions


need a help?


find thousands of online teachers now
C++ program to print pattern of stars till N numbe... >>
<< C++ Program to print a Pascal Triangle upto N dept...