Pattern
Pattern
Pyramid Program
*
**
***
****
*****
Let’s write the java code to understand this pattern
better.
1 public class Edureka
2 {
3 public static void pyramidPattern(int n)
4 {
5 for (int i=0; i<n; i++) //outer loop for number of rows(n
6 loop for spaces
7 {
9 }
for (int j=0; j<=i; j++ ) //inner loop for number of col
10 {
11 System.out.print("* "); //print star
12 }
13
14 System.out.println(); //ending line after each row
15 }
16 }
17
18 public static void main(String args[]) //driver function
19 {
20 int n = 5;
21 pyramidPattern(n);
22 }
23}
10 }
12 {
14 }
10 }
12 {
14 }
16 }
17 }
18 public static void main(String args[])
19 {
20 int n = 5;
printStars(n);
21 }
22}
4. Diamond Shape Pattern Program in Java
Enter the number of rows: 5
*
***
*****
*******
*********
*******
*****
***
*
1 import java.util.Scanner;
2 public class Edureka
3 {
4 public static void main(String args[])
5 {
6 int n, i, j, space = 1;
7 System.out.print("Enter the number of rows: ");
8 Scanner s = new Scanner(System.in);
9 n = s.nextInt();
10space = n - 1;
11for (j = 1; j<= n; j++)
12{
13for (i = 1; i<= space; i++)
14{
15System.out.print(" ");
16}
17space--;
18for (i = 1; i <= 2 * j - 1; i++)
19{
20System.out.print("*");
21}
22System.out.println("");
23}
24space = 1;
25for (j = 1; j<= n - 1; j++)
26{
27for (i = 1; i<= space; i++)
28{
29System.out.print(" ");
30}
31space++;
32for (i = 1; i<= 2 * (n - j) - 1; i++)
33{
34System.out.print("*");
35}
36System.out.println("");
37}
38}
39}
5. Downward Triangle Star Pattern
Enter the number of rows: 5
*****
****
***
**
*
1 import java.util.Scanner;
2 public class Edureka
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7
System.out.println("Enter the number of rows: "); //take
8 input from user
9
10 int rows = sc.nextInt();
11
12 for (int i= rows-1; i>=0 ; i--)
13 {
14 for (int j=0; j<=i; j++)
15 {
16 System.out.print("*" + " ");
17 }
18 System.out.println();
19 }
20 sc.close();
21 }
22 }
6. Mirrored Right Triangle Star Program
Enter the number of rows: 5
*
**
***
****
*****
******
1 import java.util.Scanner;
2 public class Edureka
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7
8 System.out.println("Enter number of rows: "); // takes
9 input from user
10
11 int rows = sc.nextInt();
12 for (int i= 0; i<= rows; i++)
13 {
14 for (int j=1; j<=rows-i; j++)
15 {
16 System.out.print(" ");
17 }
18 for (int k=0;k<=i;k++)
19 {
20 System.out.print("*");
21 }
22 System.out.println("");
23 }
24 sc.close();
25
26 }
27}
7. Reversed Pyramid Star Pattern
Enter the number of rows: 5
*****
****
***
**
*
1 import java.util.Scanner;
2 public class Edureka
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7 System.out.println("Enter the number of rows: ");
8
9 int rows = sc.nextInt();
10 for (int i= 0; i<= rows-1 ; i++)
11 {
12 for (int j=0; j<=i; j++)
13 {
14 System.out.print(" ");
15 }
16 for (int k=0; k<=rows-1-i; k++)
17 {
18 System.out.print("*" + " ");
19 }
20 System.out.println();
21 }
22 sc.close();
23
24}
25}
8. Right down Mirror Star Pattern
Enter the number of rows: 5
*****
****
***
**
*
1 import java.util.Scanner;
2 public class Edureka
3 {
4
5 public static void main(String[] args)
6 {
7 Scanner sc = new Scanner(System.in); // takes input
8 System.out.println("Enter number of rows: ");
9 int rows = sc.nextInt();
10 for (int i= rows; i>= 1; i--)
11 {
12 for (int j=rows; j>i;j--)
13 {
14 System.out.print(" ");
15 }
16 for (int k=1;k<=i;k++)
17 {
18 System.out.print("*");
19 }
20 System.out.println("");
21 }
22 sc.close();
23 }
24 }
1 import java.util.Scanner;
2 public class Edureka
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7 System.out.println("Enter the number of rows: ");
8
9 int rows = sc.nextInt();
10 for (int i= 0; i<= rows-1 ; i++)
{
11 for (int j=0; j<=i; j++) { System.out.print("*"+ " "); } S
12i=rows-1; i>=0; i--)
13 {
14 for(int j=0; j <= i-1;j++)
15 {
16 System.out.print("*"+ " ");
17 }
18 System.out.println("");
19 }
20 sc.close();
21 }
22}
10. Left Triangle Pascal’s
Enter the number of rows: 5
*
**
***
****
*****
****
***
**
*
1 import java.util.Scanner;
2 public class Edureka
3 {
4
5 public static void main(String[] args)
6 {
7 Scanner sc = new Scanner(System.in);
8 System.out.println("Enter the number of rows: ");
9
10 int rows = sc.nextInt();
11 for (int i= 1; i<= rows ; i++)
12 {
13 for (int j=i; j <rows ;j++)
14 {
15 System.out.print(" ");
16 }
17 for (int k=1; k<=i;k++) { System.out.print("*"); } Syste
18i=rows; i>=1; i--)
19 {
21 {
22 System.out.print(" ");
23 }
25 {
26 System.out.print("*");
27 }
28 System.out.println("");
29 }
30 sc.close();
31 }
32}
1 import java.util.Scanner;
2 public class Edureka
3 {
4 // Java program to print alphabet A pattern
5 void display(int n)
6 {
7 // Outer for loop for number of lines
8 for (int i = 0; i<=n; i++) {
9 // Inner for loop for logic execution
10 for (int j = 0; j<= n / 2; j++) {
11 // prints two column lines
12 if ((j == 0 || j == n / 2) && i != 0 ||
13 // print first line of alphabet
14 i == 0 && j != n / 2 ||
15 // prints middle line
16 i == n / 2)
17 System.out.print("*");
18 else
19 System.out.print(" ");
20 }
21 System.out.println();
22 }
23 }
24 public static void main(String[] args)
25 {
26 Scanner sc = new Scanner(System.in);
27 Edureka a = new Edureka();
28 a.display(7);
29 }
30 }
13. Triangle Star pattern
Enter the number of rows: 5
*
**
* *
* *
*********
1 import java.util.Scanner;
2 public class Edureka
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7
8 System.out.println("Enter the number of rows: ");
9
10 int rows = sc.nextInt();
11
12 for (int i=1; i<= rows ; i++)
13 {
14 for (int j = i; j < rows ; j++) {
15 System.out.print(" ");
16 }
17 for (int k = 1; k <= (2*i -1) ;k++) {
18 if( k==1 || i == rows || k==(2*i-1)) {
19 System.out.print("*");
20 }
21 else {
22 System.out.print(" ");
23 }
24 }
25 System.out.println("");
26 }
27 sc.close();
28 }
29 }
1 import java.util.Scanner;
2 public class Edureka
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7 System.out.println("Enter the number of rows: ");
8
9 int rows = sc.nextInt();
10 for (int i=rows; i>= 1 ; i--)
11 {
12 for (int j = i; j < rows ; j++) {
13 System.out.print(" ");
14 }
15 for (int k = 1; k <= (2*i -1) ;k++) {
16 if( k==1 || i == rows || k==(2*i-1)) {
17 System.out.print("*");
18 }
19 else {
20 System.out.print(" ");
21 }
22 }
23 System.out.println("");
24 }
25 sc.close();
26}
27}
15. Diamond Star Pattern
Enter the number of rows: 5
*
**
* *
* *
* *
* *
* *
**
*
1 import java.util.Scanner;
2 public class Edureka
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7
8 System.out.println("Enter the number of rows: ");
9
10 int rows = sc.nextInt();
11 for (int i=1; i<= rows ; i++) { for (int j = rows; j > i ; j--) {
12 System.out.print(" ");
13 }
14 System.out.print("*");
15 for (int k = 1; k < 2*(i -1) ;k++) { System.out.print(" "); }
16else { System.out.println("*"); } } for (int i=rows-1; i>= 1 ; i-
17 {
19 System.out.print(" ");
20 }
21 System.out.print("*");
23 System.out.print(" ");
}
24 if( i==1)
25 System.out.println("");
26 else
27 System.out.println("*");
28 }
29 sc.close();
30}
31}
1 import java.util.Scanner;
2
3 public class Edureka
4 {
5 public static void main(String[] args) {
6 int i, j, k = 1;
7 for (i = 1; i <= 5; i++) {
8 for (j = 1; j< i + 1; j++) {
9 System.out.print(k++ + " ");
10 }
11
12 System.out.println();
13 }
14 }
15
16 }
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 import java.util.Scanner;
2
3 public class Edureka
4 {
5 public static void main(String[] args) {
6
7 int n = 5;
8
9 for (int i = 0; i < n; i++) {
10 int number = 1;
11 System.out.printf("%" + (n - i) * 2 + "s", "");
12 for (int j = 0; j <= i; j++) {
13 System.out.printf("%4d", number);
14 number = number * (i - j) / (j + 1);
15 }
16 System.out.println();
17 }
18
19 }
20
21}
4. Diamond Pattern Program in Java
1
212
32123
4321234
32123
212
1
1 import java.util.Scanner;
2
3 public class Edureka
4 {
5 public static void main(String[] args) {
6 for (int i = 1; i <= 4; i++)
7 {
8 int n = 4;
9
10 for (int j = 1; j<= n - i; j++) { System.out.print(" "); } fo
11 {
12 System.out.print(k);
13 }
14 for (int l = 2; l <= i; l++) { System.out.print(l); } System
15>= 1; i--)
16 {
17 int n = 3;
18
19 for (int j = 0; j<= n - i; j++) { System.out.print(" "); } fo
20 {
21 System.out.print(k);
22 }
12 {
14 {
15 System.out.print(i+" ");
16 }
17 System.out.println();
18 }
19 sc.close();
20 }
21}
Next
5
54
543
5432
54321
1 import java.util.Scanner;
2 public class Edureka
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7
8 //Taking rows value from the user
9
10System.out.println("Enter the number of rows: ");
11
12int rows = sc.nextInt();
13for (int i = rows; i >= 1; i--)
14{
15for (int j = rows; j >= i; j--)
16{
17System.out.print(j+" ");
18}
19
20System.out.println();
21}
22sc.close();
23}
24}
1 import java.util.Scanner;
2 public class Edureka
3 {
4
5 public static void main(String[] args)
6 {
7 Scanner sc = new Scanner(System.in);
8
9 System.out.println("Enter the number of rows: ");
10
11int rows = sc.nextInt();
12
13for (int i = 1; i <= rows; i++) { for (int j = i; j >= 1; j--)
14 {
15 System.out.print(j+" ");
16 }
17
18 System.out.println();
19}
20sc.close();
21}
22}
1 import java.util.Scanner;
2 public class Edureka
3 {
4
5 public static void main(String[] args)
6 {
7 Scanner sc = new Scanner(System.in);
8
9 System.out.println("Enter the number of rows: ");
10
11 int rows = sc.nextInt();
12
13 for (int i = 1; i <= rows; i++)
14 {
15 int num;
16
17 if(i%2 == 0)
18 {
19 num = 0;
20
21 for (int j = 1; j <= rows; j++)
22 {
23 System.out.print(num);
24
25 num = (num == 0)? 1 : 0;
26 }
27 }
28 else
29 {
30 num = 1;
31
32 for (int j = 1; j <= rows; j++)
33 {
34 System.out.print(num);
35
36 num = (num == 0)? 1 : 0;
37 }
38 }
39
40 System.out.println();
41 }
42
43 sc.close();
44 }
45}
46
1 import java.util.Scanner;
2
3 public class Edureka
4 {
5 public static void main(String[] args)
6 {
7 Scanner sc = new Scanner(System.in);
8
9 System.out.println("Enter the number of rows: ");
10
11 int rows = sc.nextInt();
12 for (int i = 1; i <= rows; i++)
13 {
14 for (int j = 1; j <= i; j++)
15 {
16 if(j%2 == 0)
17 {
18 System.out.print(0);
19 }
20 else
21 {
22 System.out.print(1);
23 }
24 }
25
26 System.out.println();
27 }
28
29 sc.close();
30 }
31}
12345
2345
345
45
5
45
345
2345
12345
1 import java.util.Scanner;
2 public class Edureka
3 {
4 public static void main(String[] args)
5 {
6
7 int n = 5;
8
9 for (int i = 1; i <= n; i++)
10 {
11 for (int j = 1; j < i; j++)
12 {
13 System.out.print(" ");
14 }
15
16 for (int k = i; k <= n; k++) { System.out.print(k+" "); }
17i >= 1; i--)
18 {
20 {
21 System.out.print(" ");
22 }
24 {
25 System.out.print(k+" ");
26 }
27
28 System.out.println();
}
29
30 }
31}
1 import java.util.Scanner;
2
3 public class Edureka
4 {
5 public static void main(String[] args)
6 {
7 int alphabet = 65;
8 for (int i = 0; i<= 5; i++)
9 {
10 for (int j = 0; j <= i; j++)
11 {
12 System.out.print((char) alphabet + " ");
13 }
14 alphabet++;
15 System.out.println();
16 }
17 }
18}
19
20
1 import java.util.Scanner;
2
3 public class Edureka
4 {public static void main(String[] args)
5 {
6 for (int i = 5; i >= 0; i--)
7 {
8 int alphabet = 65;
9 for (int j = 0; j <= i; j++)
10 {
11 System.out.print((char) (alphabet + j) + " ");
12 }
13 System.out.println();
14}
15for (int i = 0; i<= 5; i++)
16{
17 int alphabet = 65;
18 for (int j = 0; j <= i; j++)
19 {
20 System.out.print((char) (alphabet + j) + " ");
21 }
22 System.out.println();
23}
24}
25}
26
27
4. Triangle Character Pattern Program in Java
A
AB
ABC
ABCD
ABCDE
ABCDEF
8 System.out.print(" ");
9 }
11 {
13 }
14 System.out.println();
15 }
}
16}
1 import java.util.Scanner;
2
3 public class Edureka
4 {public static void main(String[] args) {
5 char[] letter = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
6 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
7 'W', 'X', 'Y', 'Z' };
8 int letter_number = 0;
9 String[] diamond = new String[26]; // array of strings
10 System.out.print("Enter a Character between A to Z : ");
11
12 Scanner reader = new Scanner(System.in);
13 try {
14 char user_letter = reader.next("[A-Z]").charAt(0);
15 // search for letter number in the array letter
16 for (int i = 0; i < letter.length; i++) {
17 if (letter[i] == user_letter) {
18 letter_number = i;
19 break;
20 }
21 }
22
23 // construct diamond
24 for (int i = 0; i <= letter_number; i++) {
25 diamond[i] = "";
26 // add initial spaces
27 for (int j = 0; j < letter_number - i; j++) {
28 diamond[i] += " ";
29 }
30
31 // add letter
32 diamond[i] += letter[i];
33
34 // add space between letters
35 if (letter[i] != 'A') {
for (int j = 0; j < 2 * i - 1; j++) { diamond[i] += " "; }
letter[i]; } // Draw the first part of the diamond System.out
36letter_number - 1; i >= 0; i--)
37 {
38 // Draw the second part of the diamond
39 // Writing the diamondArray in reverse order
40 System.out.println(diamond[i]);
41 }
42 } catch (Exception e) {
43 e.printStackTrace();
44 } finally {
45 reader.close();
46 }
47
48}
49}