Java print binary – Java Program to Print Square with Alternate row Binary Number Pattern

Program to Print Square with Alternate row Binary Number Pattern In Java

Java print binary: In the previous article, we have discussed Java Program to Print Square with Same Number Pattern

In this article we will see how to print Square with Alternate row Binary Number Pattern.

Exmaple-1

Enter number of row: 6
Enter number of column: 6

111111
000000
111111
000000
111111
000000
Exmaple-2

Enter number of row: 5
Enter number of column: 4

11111
00000
11111
00000

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Approach:-

  • Enter number of row and number of binary digits in each row and store it in variable b row and col respectively.
  • Take a  outer for loop to iterate the Rows.
  • Take an inner for loop inside outer loop to iterate the columns.
  • After the iteration of one row move to next row.

Java Code to Print Square with Alternate row Binary Number Pattern

import java.util.*;
class Main
{
    public static void main(String[] args) 
    {
        Scanner read=new Scanner(System.in);
        int i, j, row, col;
        System.out.print("Enter the Number of Row :");
        row=read.nextInt();
        System.out.print("Enter the Number of Columns :");
        col=read.nextInt();

        for (i=1;i<=row ;i++ ) 
        {
        	
            for (j=1;j<=col ;j++ ) 
            {
            	if (i%2 !=0)
            	 {
            	      System.out.print("1");
            	 }
            	else 
            	{
            	    System.out.print("0");
            	}
            }
            System.out.println("");
            
        }
    }
}

Output:

Enter number of row: 6
Enter number of column: 6

111111
000000
111111
000000
111111
000000

C Code to Print Square with Alternate row Binary Number Pattern

#include <stdio.h>
int main()
{
    int row, col, i, j;

    /* Input rows and columns from user */
    printf("Enter number of row: ");
    scanf("%d", &row);
    printf("Enter number of column: ");
    scanf("%d", &col);

    for(i=1; i<=row; i++)
    {
        for(j=1; j<=col; j++)
        {
            // Print 1 if current row is odd
            if(i%2 == 1)
            {
                printf("1");
            }
            else
            {
                printf("0");
            }
        }

        printf("\n");
    }

    return 0;
}
 



Output:

Enter number of row: 6
Enter number of column: 6

111111
000000
111111
000000
111111
000000

C++ Code to Print Square with Alternate row Binary Number Pattern

#include<iostream>
using namespace std;

int main()
{
    int i, j, row, col;
     //Enter the Number of Rows 
    cout << " Enter the Number of Row ";
    cin >> row;
    //Enter the number of columns
    cout << "Enter the Number of Column ";
    cin >> col;
     //for loop to iterate the rows and column
    for(i = 1; i <= row; i++)
    {
    	for(j = 1; j <= col; j++)
        {
            if(i % 2 != 0)
            {
                cout << "1";
            }
            else
            {
                cout << "0";
            }       	
        }
        cout << "\n";
    }
        
 	return 0;
}
Output:

Enter number of row: 6
Enter number of column: 6

111111
000000
111111
000000
111111
000000

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Try these:

  • Java Program To Print Binary Number Pattern
  • How To Print Square Pattern In Java
  • How To Print Binary Number In Java
  • Binary Pattern In Java
  • Odd Square Pattern In Java
  • Square Pattern Program In Java
  • Print Binary In Java

Related Java Number Pattern Programs: