WAP to print the above pattern in JAVA
Plzzz answer my Q
Answers
Java Patterns
Pattern questions in Java require some analysis on our part.
Here we notice that the first line cannot be set as a part of a loop. So we would print it as it is.
The next 8 lines have their first digits in a pattern. In each of these 8 rows, the number is printed exactly once.
We run a loop, with say a variable i, for 8 times. And in each of these 8 times, we print i once, and the value of i+1 is then to be printed i times.
Here is a Java program for the same.
public class Pattern //Creating Class
{
public static void main(String[] args) //The main method
{
System.out.println("1"); //Printing the first line
for(int i=1;i<9;i++) //Setting the outer loop to run 8 times from 1 to 8
{
System.out.print(i+" "); //Printing the value of i once
int k=i+1; //Setting k=i+1 for printing it i times
for(int j=0;j<i;j++) //Running a loop i times
{
System.out.print(k+" "); //Printing the value of k
}
System.out.println(); //Printing a blank line after each row
}
}
}