Home » C++ programs » C++ Most popular & searched programs

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

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.



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

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.

Easy it was. Right?






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.