WAP in Java to print this pattern
1
12
123
1234
12345
1234
123
12
1
Answers
Question:
- To print in java the following pattern
1
12
123
1234
12345
1234
123
12
1
_______________________
Answer:
import java.util.*;
class Code
{
public static void main (String ar [])
{
/*Solved by
*@swetank232894
*/
for (int i=1;i<=5;i++) //This is the code of first part
{
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println()
}
for (int i=4;i>=1;i--) //This is the code of second part
{
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
__________________
Explanation:
This pattern can be divided into 2 parts,
1st part :
1
12
123
1234
12345
2nd part:
1234
123
12
1
So the program will also be divided into two part, (refer to the code given above)
in the first part, i loop is running from 1 to 5
inside which the j loop runs from 1 to i
Since i need to print the pattern in this format:-
1
12
...12345
i will print j in the same line using System.out.print() which prints in the same line
Then i need to leave line which is done using System.out.println() means to leave line
Second part is also same but it is just opposite, here the i loop is running from 5 to 1
Rest are same.
___________________
Output:-
1
12
123
1234
12345
1234
123
12
1
Explanation:
this is you proper answer