0% found this document useful (0 votes)
16 views

Pattern

The document contains 14 code examples that print different star patterns in Java using for loops. The patterns include pyramid, triangle, diamond, mirrored, pascal's triangle, and alphabet shapes printed to the console using nested for loops and conditional logic. Each example includes the code to take user input for number of rows, nested for loops to control number of rows and columns, and print statements to output the stars in the desired pattern.

Uploaded by

yedogo5597
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Pattern

The document contains 14 code examples that print different star patterns in Java using for loops. The patterns include pyramid, triangle, diamond, mirrored, pascal's triangle, and alphabet shapes printed to the console using nested for loops and conditional logic. Each example includes the code to take user input for number of rows, nested for loops to control number of rows and columns, and print statements to output the stars in the desired pattern.

Uploaded by

yedogo5597
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 69

1.

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 {

8 System.out.print(" "); //print space

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}

2. Right Triangle Star Pattern


*
**
***
****
*****
1 public class Edureka
2 {
3 public static void rightTriangle(int n)
4 {
5 int i, j;
6 for(i=0; i<n; i++) //outer loop for number of rows(n) {
7 loop for spaces
8 {

9 System.out.print(" "); // printing space

10 }

11 for(j=0; j<=i; j++) // inner loop for columns

12 {

13 System.out.print("* "); // print star

14 }

15 System.out.println(); // ending line after each row


}
16 }
17 public static void main(String args[])
18 {
19 int n = 5;
20 rightTriangle(n);
21 }
22}
3. Left Triangle Star Pattern
*
**
***
****
*****
1 public class Edureka
2 {
3 public static void printStars(int n)
4 {
5 int i, j;
6 for(i=0; i<n; i++) //outer loop for number of rows(n) {
7 loop for spaces
8 {

9 System.out.print(" "); // printing space

10 }

11 for(j=0; j<=i; j++) // inner loop for columns

12 {

13 System.out.print("* "); // print star

14 }

15 System.out.println(); // ending line after each row

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 }

9. Right Pascal’s Triangle


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 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 {

20 for(int j=i; j<=rows;j++)

21 {

22 System.out.print(" ");

23 }

24 for(int k=1; k<i ;k++)

25 {

26 System.out.print("*");

27 }

28 System.out.println("");
29 }
30 sc.close();
31 }
32}

11. Sandglass 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=i; k<=rows-1; k++) { System.out.print("*" +
i= rows-1; i>= 0; i--)
17 {
18 for (int j=0; j< i ;j++)
19 {
20 System.out.print(" ");
21 }
22 for (int k=i; k<=rows-1; k++)
23 {
24 System.out.print("*" + " ");
25 }
26 System.out.println("");
27 }
28 sc.close();
29 }
30}
12. Alphabet A Pattern
**
**
***
**
**
**

Java Certification Training Course


 Instructor-led Sessions
 Real-life Case Studies
 Assignments
 Lifetime Access
Explore Curriculum

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 }

14. Down triangle


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=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 {

18 for (int j = rows; j > i ; j--) {

19 System.out.print(" ");

20 }

21 System.out.print("*");

22 for (int k = 1; k < 2*(i -1) ;k++) {

23 System.out.print(" ");
}
24 if( i==1)
25 System.out.println("");
26 else
27 System.out.println("*");
28 }
29 sc.close();
30}
31}

Now that we have implemented star pattern


programs in Java. Let us move further and
implement some Numeric patterns.
Numeric Pattern in Java

1. Simple number program


1
12
123
1234
12345

1 public class Edureka


2 {
3 public static void printNums(int n)
4 {
5 int i, j,num;
6
7 for(i=0; i<n; i++) // outer loop for rows
8 {
9 num=1;
10 for(j=0; j<=i; j++) // inner loop for rows
11 {
12 // printing num with a space
13 System.out.print(num+ " ");
14
15 //incrementing value of num
16 num++;
17 }
18
19 // ending line after each row
20 System.out.println();
21 }
22 }
23 public static void main(String args[])
24 {
25 int n = 5;
26 printNums(n);
27 }
28}
2. Number Pattern Program in java
1
23
456
7 8 9 10
11 12 13 14 15

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 }

3. Pascal’s Triangle Program in Java

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 }

23 for (int l = 2; l <= i; l++)


{
24 System.out.print(l);
25 }
26
27 System.out.println();
28 }
29
30 }
31}

5. Number Pattern Programs in Java


Enter the number of rows: 5
1
22
333
4444
55555
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); //Taking rows va
8 from the user
9 System.out.println("Enter the number of rows: ");

10 int rows = sc.nextInt();

11 for (int i = 1; i <= rows; i++)

12 {

13 for (int j = 1; j <= i; j++)

14 {

15 System.out.print(i+" ");

16 }
17 System.out.println();
18 }
19 sc.close();
20 }
21}

6. Descending order Pattern


Enter the number of rows: 5
Programming & Frameworks Training

FULL STACK WEB DEVELOPMENT INTERNSHIP


PROGRAM
Full Stack Web Development Internship Program
Reviews
5(28099)

JAVA CERTIFICATION TRAINING COURSE


Java Certification Training Course
Reviews
4(72270)

FLUTTER APPLICATION DEVELOPMENT COURSE


Flutter Application Development Course
Reviews
5(10895)

PYTHON SCRIPTING CERTIFICATION TRAINING


Python Scripting Certification Training
Reviews
5(13390)

SPRING FRAMEWORK CERTIFICATION COURSE


Spring Framework Certification Course
Reviews
5(11192)

NODE.JS CERTIFICATION TRAINING COURSE


Node.js Certification Training Course
Reviews
5(9428)

PYTHON DJANGO CERTIFICATION TRAINING


COURSE
Python Django Certification Training Course
Reviews
5(8272)

ADVANCED JAVA CERTIFICATION TRAINING


Advanced Java Certification Training
Reviews
5(6419)
PHP & MYSQL WITH MVC FRAMEWORKS
CERTIFICATION TRAINING
PHP & MySQL with MVC Frameworks Certification
Training
Reviews
5(4616)

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}

7. Right Triangle Numeric Pattern


Enter the number of rows: 5
1
21
321
4321
54321

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}

8. Binary Number Pattern


Enter the number of rows: 5
10101
01010
10101
01010
10101

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

9. Zeros/ ones Pattern Programs


Enter the number of rows: 5
1
10
101
1010
10101

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}

10. Diamond Numeric Pattern

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 {

19 for (int j = 1; j < i; j++)

20 {

21 System.out.print(" ");

22 }

23 for (int k = i; k <= n; k++)

24 {

25 System.out.print(k+" ");

26 }

27
28 System.out.println();
}
29
30 }
31}

Now that we have implemented numeric


pattern programs in Java. Let us move further and
implement some character/ alphabet patterns.
Alphabet/ Character Patterns in Java

1. Right Alphabetic triangle


A
AB
ABC
ABCD
ABCDE
ABCDEF
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 + j) + " ");
13 }
14 System.out.println();
15 }
16 }
17}
18

2. Alphabet/ Character Pattern Programs


A
BB
CCC
DDDD
EEEEE
FFFFFF

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

3. K Shape Character Pattern Program


ABCDEF
ABCDE
ABCD
ABC
AB
A
A
AB
ABC
ABCD
ABCDE
ABCDEF

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

Java Certification Training Course


Weekday / Weekend BatchesSee Batch Details

1 public class Edureka


2 {
3 public static void main(String[] args)
4 {
5 for (int i = 0; i <= 5; i++) { int alphabet = 65; for (int j =
6 j--)
7 {

8 System.out.print(" ");

9 }

10 for (int k = 0; k <= i; k++)

11 {

12 System.out.print((char) (alphabet + k) + " ");

13 }

14 System.out.println();

15 }
}
16}

5. Diamond Pattern in Java


Enter a Character between A to Z : F
A
BB
C C
D D
E E
F F
E E
D D
C C
BB
A

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}

You might also like