Write a program in Java to display the following pattern. The program should
accept the rows to be printed using the scanner class. For example, if the user
enters 7 the following pattern gets printed
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
1 0 1 0 1 0
1 0 1 0 1 0 1
Answers
Answered by
1
Answer:
n=Sc.nextInt(); // no. of rows taken as input
for(i=1;i<=n;i++)
{
if(i%2==0)
System.out.println("0");
else
System.out.println("1");
}
Answered by
1
Question:-
Write a program in Java to print the following pattern.
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
1 0 1 0 1 0
1 0 1 0 1 0 1
Solution:-
This is the shortest code.
import java.util.*;
class Program
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int i,j, n;
System.out.print("Enter the number of rows: ");
n=sc.nextInt();
System.out.println("Here is your pattern: ");
for(i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
System.out.print(j%2+" ");
System.out.println();
}
}
}
Similar questions