Here are some tricks to print * pattern
If you can print pattern like this then you are genius
In above pattern first loop for (int i = 1; i <= 5; i++) used for rows
and second loop for (int j = 1; j <= 5; j++) is used for columns
Now if we want to print pattern like this
for this we need to put a condition before printing *
Here are some other patterns you can try.
Change the condition for the required pattern
If you can print pattern like this then you are genius
NOTE : Now all you need to print any other pattern is just satisfy a condition before printing *
Java code to generate above pattern is given below.
Java code to generate above pattern is given below.
save it with name Pattern.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.trickyjava.pattern; public class Pattern { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { System.out.print(" * "); } System.out.println(); } } } |
In above pattern first loop for (int i = 1; i <= 5; i++) used for rows
and second loop for (int j = 1; j <= 5; j++) is used for columns
Now if we want to print pattern like this
for this we need to put a condition before printing *
So now we will learn how to make condition to print pattern
Here is explanation for the above pattern
NOTE : we will make condition based on ( j + i ) and ( j - i ) for any pattern.
Now java code to print the above pattern is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.trickyjava.pattern; public class Pattern { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { if (i + j <= 6) { System.out.print(" * "); } else { System.out.print(" "); } } System.out.println(); } } } |
Here are some other patterns you can try.
Change the condition for the required pattern
Some more patterns
Change no. of rows or columns to print required pattern
No comments:
Post a Comment