Java Program to Print 1 and 0 in Alternative Rectangle Rows

Write a Java program to print 1 and 0 in alternative rectangle rows using for loop.

import java.util.Scanner;

public class AlternativeRows1and01 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);	
		
		System.out.print("Enter Numbers of Rows = ");
		int rows = sc.nextInt();
		
		System.out.print("Enter Numbers of Columns = ");
		int columns = sc.nextInt();
		
		System.out.println("Printing 1 and 0 Numbers in Alternative Rows");
		
		for (int i = 1 ; i <= rows; i++ ) 
		{
			for (int j = 1 ; j <= columns; j++ ) 	
			{
				if(i % 2 != 0) {
					System.out.printf("1");
				}
				else {
					System.out.printf("0");
				}
			}
			System.out.println();
		}
	}
}
Java Program to Print 1 and 0 in Alternative Rows 1

This Java program prints a rectangle of 1 and 0 in alternative rows using a while loop.

import java.util.Scanner;

public class AlternativeRows1and02 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);	
		
		System.out.print("Enter Numbers of Rows and Columns = ");
		int rows = sc.nextInt();
		int columns = sc.nextInt();
		
		System.out.println("Printing 1 and 0 Numbers in Alternative Rows");
		
		for (int i = 1 ; i <= rows; i++ ) 
		{
			for (int j = 1 ; j <= columns; j++ ) 	
			{
				System.out.printf("%d", i % 2);
			}
			System.out.println();
		}
	}
}
Enter Numbers of Rows and Columns = 9 14
Printing 1 and 0 Numbers in Alternative Rows
11111111111111
00000000000000
11111111111111
00000000000000
11111111111111
00000000000000
11111111111111
00000000000000
11111111111111

Java Program to Print 1 and 0 in Alternative Rows of a rectangle using do while loop.

import java.util.Scanner;

public class AlternativeRows1and03 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);	
		
		System.out.print("Enter Numbers of Rows and Columns = ");
		int rows = sc.nextInt();
		int columns = sc.nextInt();
		
		System.out.println("Printing 1 and 0 Numbers in Alternative Rows");
		
		int j, i = 1;
		
		while (i <= rows ) 
		{
			j = 1 ;
			while(j <= columns) 	
			{
				System.out.printf("%d", i % 2);
				j++ ;
			}
			System.out.println();
			i++;
		}
	}
}
Enter Numbers of Rows and Columns = 10 20
Printing 1 and 0 Numbers in Alternative Rows
11111111111111111111
00000000000000000000
11111111111111111111
00000000000000000000
11111111111111111111
00000000000000000000
11111111111111111111
00000000000000000000
11111111111111111111
00000000000000000000

This Java example prints the rectangle of 0 and 1 in alternative rows.

import java.util.Scanner;

public class AlternativeRows1and04 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);	
		
		System.out.print("Enter Numbers of Rows and Columns = ");
		int rows = sc.nextInt();
		int columns = sc.nextInt();
		
		System.out.println("Printing 0 and 1 Numbers in Alternative Rows");
		
		for (int i = 1 ; i <= rows; i++ ) 
		{
			for (int j = 1 ; j <= columns; j++ ) 	
			{
				if(i % 2 != 0) {
					System.out.printf("0");
				}
				else {
					System.out.printf("1");
				}
			}
			System.out.println();
		}
	}
}
Enter Numbers of Rows and Columns = 12 25
Printing 0 and 1 Numbers in Alternative Rows
0000000000000000000000000
1111111111111111111111111
0000000000000000000000000
1111111111111111111111111
0000000000000000000000000
1111111111111111111111111
0000000000000000000000000
1111111111111111111111111
0000000000000000000000000
1111111111111111111111111
0000000000000000000000000
1111111111111111111111111

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.