Q:

Java patterns program 4)

belongs to collection: Java programs to print patterns

0

Program 4) Print the following pattern


1
10
101
1010
10101

 

All Answers

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

Program

import java.util.Scanner;
 
public class Pattern4
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of rows: ");
         
        int rows = sc.nextInt();
         
        System.out.println("Here is your pattern....!!!");
         
        for (int i = 1; i <= rows; i++) 
        {
            for (int j = 1; j <= i; j++)
            {
                if(j%2 == 0)
                {
                    System.out.print(0);
                }
                else
                {
                    System.out.print(1);
                }
            }    
            System.out.println();
        }
        sc.close();
    }
}

Output

 
Enter number of rows: 5
Here is your pattern....!!!
1
10
101
1010
10101

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

total answers (1)

Java patterns program 5)... >>
<< Java patterns program 3)...