InterviewSolution
| 1. |
WAP to print the above pattern in JAVA Plzzz answer my Q |
Answer» JAVA PatternsPattern 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 { System.out.print(k+" "); //Printing the value of k } System.out.println(); //Printing a blank line after each row } } } |
|