├── Readme.txt ├── Patterns.txt └── Main.java /Readme.txt: -------------------------------------------------------------------------------- 1 | The Main.java contains code for 11 pattern problems, The patterns are mentioned in Patterns.txt file. 2 | Learning to write logic for these patterns is important becasue by building the logic for these patterns we can understand 3 | and master the loops and also improve our logic thinking and logic building skills. 4 | Try to see the pattern and build the logic without looking at the code but if you get stuck anywhere the solution for every 5 | pattern is provided in Main.java code. The code might be in Java but the logic will be same for the patterns in all the 6 | programming languages. 7 | Enjoy learning. 8 | -------------------------------------------------------------------------------- /Patterns.txt: -------------------------------------------------------------------------------- 1 | Pattern 1: 2 | * * * * * 3 | * * * * * 4 | * * * * * 5 | * * * * * 6 | * * * * * 7 | 8 | Pattern 2: 9 | * 10 | * * 11 | * * * 12 | * * * * 13 | * * * * * 14 | 15 | Pattern 3: 16 | * * * * * 17 | * * * * 18 | * * * 19 | * * 20 | * 21 | 22 | Pattern 4: 23 | 1 24 | 1 2 25 | 1 2 3 26 | 1 2 3 4 27 | 1 2 3 4 5 28 | 29 | Pattern 5: 30 | * 31 | * * 32 | * * * 33 | * * * * 34 | * * * * * 35 | * * * * 36 | * * * 37 | * * 38 | * 39 | 40 | Pattern 6: 41 | ***** 42 | * * 43 | * * 44 | ***** 45 | 46 | Pattern 7: 47 | * 48 | * * 49 | * * * 50 | * * * * 51 | * * * * * 52 | 53 | Pattern 8: 54 | * 55 | ** 56 | *** 57 | **** 58 | ***** 59 | 60 | Pattern 9: 61 | 1 2 3 4 5 62 | 1 2 3 4 63 | 1 2 3 64 | 1 2 65 | 1 66 | 67 | Pattern 10: 68 | 1 69 | 2 3 70 | 4 5 6 71 | 7 8 9 10 72 | 11 12 13 14 15 73 | 74 | Pattern 11: 75 | 1 76 | 0 1 77 | 1 0 1 78 | 0 1 0 1 79 | 1 0 1 0 1 80 | -------------------------------------------------------------------------------- /Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | System.out.println("Star(*) pattern problems:"); 4 | pattern1(5); 5 | pattern2(5); 6 | pattern3(5); 7 | pattern3_M2(5); 8 | pattern4(5); 9 | pattern5(5); 10 | pattern6(); 11 | pattern7(5); 12 | pattern8(5); 13 | pattern9(5); 14 | pattern10(5); 15 | pattern11(5); 16 | } 17 | 18 | //Pattern 1 19 | static void pattern1(int n) { 20 | System.out.println("Pattern 1:"); 21 | for (int row = 1; row <= n; row++) { 22 | for (int col = 1; col <= n; col++) { 23 | System.out.print("* "); 24 | } 25 | System.out.println(); 26 | } 27 | 28 | } 29 | 30 | //Pattern 2 31 | static void pattern2(int n) { 32 | System.out.println("Pattern 2:"); 33 | for (int row = 1; row <= n; row++) { 34 | for (int col = 1; col <= row; col++) { 35 | System.out.print("* "); 36 | } 37 | System.out.println(); 38 | 39 | } 40 | } 41 | 42 | //Pattern 3 43 | static void pattern3(int n) { 44 | System.out.println("Pattern 3:"); 45 | for (int row = 1; row <= n; row++) { 46 | for (int col = 1; col <= n - row + 1; col++) { 47 | System.out.print("* "); 48 | } 49 | System.out.println(); 50 | 51 | } 52 | } 53 | //Pattern3 another method 54 | static void pattern3_M2(int n){ 55 | System.out.println("Pattern3 Method 2:"); 56 | for(int i=n; i>=1; i--){ 57 | for(int j=1;j<=i;j++){ 58 | System.out.print("* "); 59 | } 60 | System.out.println(); 61 | } 62 | 63 | } 64 | 65 | //Pattern 4 66 | static void pattern4(int n) { 67 | System.out.println("Pattern 4:"); 68 | for (int row = 1; row <= n; row++) { 69 | for (int col = 1; col <= row; col++) { 70 | System.out.print(col + " "); 71 | } 72 | System.out.println(); 73 | 74 | } 75 | } 76 | 77 | //pattern 5 78 | static void pattern5(int n) { 79 | System.out.println("Pattern 5:"); 80 | for (int row = 1; row <= n; row++) { 81 | for (int col = 1; col <= row; col++) { 82 | System.out.print("* "); 83 | } 84 | System.out.println(); 85 | } 86 | for (int row = 1; row <= n; row++) { 87 | for (int col = 1; col <= n - row ; col++) { 88 | System.out.print("* "); 89 | } 90 | System.out.println(); 91 | 92 | } 93 | } 94 | //Pattern 6 95 | static void pattern6(){ 96 | System.out.println("Pattern 6:"); 97 | int n = 4; 98 | int m = 5; 99 | for(int i=1;i<=n;i++){ 100 | for(int j = 1; j<=m; j++){ 101 | if(i == 1 || j == 1 || i == n || j ==m){ 102 | System.out.print("*"); 103 | } 104 | else{ 105 | System.out.print(" "); 106 | } 107 | } 108 | System.out.println(); 109 | } 110 | } 111 | 112 | //Pattern 7 113 | static void pattern7(int n){ 114 | System.out.println("Pattern 7:"); 115 | for(int i = 1;i<=n;i++){ 116 | for(int j = 1; j<=n-i;j++){ 117 | System.out.print(" "); 118 | } 119 | for(int j=1;j<=i;j++) 120 | System.out.print("* "); 121 | System.out.println(); 122 | } 123 | } 124 | //Pattern 8 125 | static void pattern8(int n){ 126 | System.out.println("Pattern 8:"); 127 | for(int i = 1;i<=n;i++){ 128 | for(int j = 1; j<=n-i;j++){ 129 | System.out.print(" "); 130 | } 131 | for(int j=1;j<=i;j++) 132 | System.out.print("*"); 133 | System.out.println(); 134 | } 135 | } 136 | //Pattern 9 Reverse number triangle 137 | static void pattern9(int n){ 138 | System.out.println("Pattern 9:"); 139 | for(int i=n;i>=1;i--){ 140 | for(int j=1;j<=i;j++){ 141 | System.out.print(j + " "); 142 | } 143 | System.out.println(); 144 | } 145 | } 146 | //pattern 10 147 | static void pattern10(int n){ 148 | System.out.println("Pattern 10:"); 149 | int iteration = 1; 150 | for(int i = 1; i<=n; i++){ 151 | for(int j = 1;j<=i;j++){ 152 | System.out.print(iteration + " "); 153 | iteration++; 154 | } 155 | System.out.println(); 156 | } 157 | } 158 | //pattern11 Binary triangle 159 | static void pattern11(int n){ 160 | System.out.println("Pattern 11:"); 161 | for(int i=1;i<=n;i++) { 162 | for (int j = 1; j <= i; j++){ 163 | int sum = i+j; 164 | if(sum%2 == 0){ 165 | System.out.print("1 "); 166 | } 167 | else{ 168 | System.out.print("0 "); 169 | } 170 | } 171 | System.out.println(); 172 | } 173 | } 174 | } 175 | --------------------------------------------------------------------------------