In this post, "Top 10 Frequently Asked Pattern Programs in Java", we will look at some of the most commonly asked pattern questions. These questions are one of the most common questions asked by teachers and interviewers. To understand the program to print pattern, it is important to have some knowledge and coding skills—and if you're looking for a good way to improve your coding skills in Java, then this post is for you!
Top 10 Frequently Asked Pattern Programs in Java
Java Ques 1: Program in Java to print right angle triangle using * sign.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Demosxcpattern{ public static void main(String args[]){ int row=5; System.out.println("pattern using *\n"); for(int i=0; i<row; i++) { for(int j=0; j<=i; j++) { System.out.print("* "); } System.out.println(); } } } |
Java Ques 2: Program in java to print pyramid pattern using * sign.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class Demosxcpattern{ public static void main(String args[]) { int sp,star=0; int rows=8; System.out.println("star pattern pyramid\n"); //outer loop for printing rows for(int i = 1; i <= rows; i++) { // print leading space for(sp = 1; sp <= rows-i; sp++) { System.out.print(" "); }// print star while(star != (2*i - 1)){ System.out.print("*"); star++; } star=0; // move to next row System.out.print("\n"); } } } |
Java Ques 3: Program in Java to print inverted right triangle star pattern using * sign.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Demosxcpattern{ public static void main(String args[]) { System.out.println("Pattern \n "); for(int i = 8; i > 0; i--) { /* Prints one row of triangle */ for(int j = i; j > 0; j--) { System.out.print("* "); } System.out.print("\n"); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class demosxcpascal { public static void main(String[] args) { int row=7, i, j, space, number; for(i=0; i<row; i++) { for(space=row; space>i; space--) { System.out.print(" "); } number=1; for(j=0; j<=i; j++) { System.out.print(number+ " "); number = number*(i-j)/(j+1); } System.out.print("\n"); } } } |
Java Ques 4: Program in Java to print the following pattern
010101
101010
010101
101010
010101
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public class demosxcpattern { public static void main(String args[]) { int n; System.out.println("pattern program \n"); for(int i = 1; i <= 5; i++) { if(i%2 == 0) { n = 0; for(int j = 1; j <= 6; j++) { System.out.print(n); n = (n == 0)? 1 : 0; } } else { n = 1; for(int j = 1; j <= 6; j++) { System.out.print(n); n = (n == 0)? 1 : 0; } } System.out.println(); } } } |
Output:-
Java Ques 5: Program in Java to print the following pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class demosxcpattern { public static void main(String args[]) { System.out.println("Pattern Program \n"); for(int i = 1; i <= 6; i++) { for(int j = 6-i; j < 5; j++) { System.out.print(" "); } for(int k = i; k <= 6; k++) { System.out.print(k); } System.out.println(); } } } |
Java Ques 6: Program in Java to print the following pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class demosxcpattern { public static void main(String args[]) { int t = 1; System.out.println("pattern program \n"); for(int i = 5; i >= 1; i--) { for(int j = 1; j <= i; j++) { System.out.print(t++ + " "); } System.out.println(); } } } |
Java Ques 7: Program in Java to print the following pattern
11 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public class demosxcpattern { public static void main(String[] args) { int rows = 5; System.out.println("Pattern Program"); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } //Printing lower half of the pattern for (int i = rows-1; i >= 1; i--) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } } } |
Output:-
Java Ques 8: Program in Java to print the following pattern
12 2
3 3 3
4 4 4 4
5 5 5 5 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class demosxcpattern { public static void main(String[] args) { int rows = 5; System.out.println("Pattern Program"); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(i+" "); } System.out.println(); } } } |
Java Ques 9: Program in Java to print the following pattern
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class demosxcpattern { public static void main(String[] args) { int rows = 5; System.out.println("Pattern Program \n"); for (int i = rows; i >= 1; i--) { for (int j = i; j >= 1; j--) { System.out.print(j+" "); } System.out.println(); } } } |
Java Ques 10: Program in Java to print the following pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class demosxc { public static void main(String[] args) { int rows = 5; System.out.println("Pattern Program \n"); //for upper pattern for (int i = rows; i >= 1; i--) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } //for lower for (int i = 2; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } } } |
No comments:
Post a Comment