Disclosure: When you purchase a service or a product through our links, we sometimes earn a commission.

How to Create a Checkerboard pattern Using Asterisks in Java

Checkerboard Pattern in Java
January 9, 2018

Here is a simple program to draw a checkerboard pattern in Java. There can be a different way of doing it. In this case, the program pads every other row with a space to have a zigzag effect.

To do that the program checks if the row number is even and if it is, it prints spaces at the beginning of the row. It uses the modulus operator (%) and the if/else code.

The if/else section should be placed in the outer loop to limit the space to only the beginning of the row.

Here is the Java code:


/** This program prints a checkerboard pattern using the asterisk symbol (*) using
  * rows and columns sizes are given by the user
**/
import java.util.*; //Import it to use the Scanner class
public class makeCheckerboardPattern{
   public static void checkerboard(int row, int col){
       for(int i = 1; i<=row; i++){
	       if(i%2 ==0) {
		   System.out.print("  "); //Start with space on every other row
	       }
		for(int j=1; j<=col; j++) {
		    System.out.print(" * ");
		}
		System.out.println();
	}
  }

  public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
      System.out.println("Please enter row size");
      int rowSize = input.nextInt();
      System.out.println("Please enter columnSize");
      int colSize = input.nextInt();
      System.out.println("Here is the checkerboard pattern\n");
      makeCheckerboardPattern(rowSize, colSize); //call the method
   }
}

8 comments on “How to Create a Checkerboard pattern Using Asterisks in Java

  1. A lot of thanks for all your valuable efforts on this site. Kate loves engaging in investigation and it’s really easy to understand why. I know all concerning the dynamic medium you give important secrets on this web blog and as well as foster participation from others on that content plus our daughter is always discovering a lot. Have fun with the remaining portion of the new year. You are conducting a great job.

  2. I precisely desired to appreciate you once again. I am not sure the things that I might have handled in the absence of the entire pointers provided by you on such a problem. It has been a very frightening difficulty in my circumstances, however , looking at the skilled mode you processed that made me to jump over fulfillment. I am happier for this help and in addition hope that you know what a great job you are always getting into instructing others through the use of your site. I know that you have never encountered all of us.

  3. I enjoy you because of all of the effort on this blog. Betty delights in carrying out internet research and it’s easy to see why. A lot of people hear all about the compelling form you convey advantageous guidance by means of the blog and attract contribution from other individuals about this area and my child has always been becoming educated a great deal. Enjoy the remaining portion of the new year. You’re carrying out a brilliant job.

  4. Thanks for the advice on credit repair on this excellent site. Some tips i would tell people should be to give up the particular mentality that they may buy currently and shell out later. Being a society many of us tend to try this for many issues. This includes vacation trips, furniture, along with items we wish. However, you’ll want to separate your current wants out of the needs. If you are working to boost your credit score actually you need some trade-offs. For example you’ll be able to shop online to save cash or you can look at second hand merchants instead of high-priced department stores pertaining to clothing.

  5. I am glad for writing to make you know what a incredible encounter my friend’s girl gained studying your web page. She learned plenty of pieces, which include what it’s like to possess an awesome helping mood to let other folks smoothly know just exactly several multifaceted subject matter. You truly exceeded our own expectations. Thank you for offering these necessary, safe, explanatory and even fun tips about this topic to Julie.

Leave a Reply

Your email address will not be published. Required fields are marked *