00:00
/*A05 - Pattern Printing */ /* * * * * * * * * * * * * * * and the below patters * * * * * * * * * * * * * * */ public class a_0026 { public static void main(String[] args) { for (int i =1 ; i<=4 ; i++){ /* use for next line*/ for(int j =1 ; j<=i ; j++){ /* loop start but condition depends on variable i ,above */ System.out.print("*"); } System.out.println(""); } System.out.println("next loop code below"); for (int k =1 ; k<=4 ; k++){ /* use for next line*/ for(int l=4; l>=k ; l--){ // for(int l=1; l<=(5-k) ; l++){ System.out.print("*"); } System.out.println(""); } } } // MA logic // when i =1 , then j = start from 1 to 1 // when i =2 , then j = start from 1 to 2 // top left - decrement // when i =1 , then j = start from 4 to 1 // when i =2 , then j = start from 4 to 2
/* a_0027 star pattern*/ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ public class a_0027 { public static void main(String[] args) { for(int i =1; i<=5; i++){ for(int j =1; j<=i; j++){ System.err.print("*"); } System.out.println(); } for( int k =1; k<=5; k++){ for( int l=4; l>=k; l--){ System.out.print("*"); } System.out.println(); } } } // MA logic // same logic as a_0026
/* a_0028 star pattern*/ /* * * * * * * * * * * * * * * * * * * * */ public class a_0028 { public static void main(String[] args) { for(int i=1; i<=5;i++){ for(int j=4;j>=i;j--){ // for making this condition , check the i and j value System.out.print(" "); } for(int k=1;k<=i;k++){ System.out.print("*"); } System.out.println(); } } } // same as logic a_0027 // two pattern w/o new line
/* a_0029 belwo pattern*/ /* * * * * * * * * * * * * * * * * * * * */ public class a_0029 { public static void main(String[] args) { for( int i=1; i<=5; i++){ for(int k=1; k<=i; k++){ System.out.print(" "); } for(int j=5; j>=i; j--){ System.out.print("*"); } System.err.println(); } } } // bottom left corner // top left corner w/o new line
/* a_0030 below star pattern*/ /* * * ** * *** * **** * ***** * **** * *** * ** * * */ public class a_0030 { public static void main(String[] args) { for(int i =1; i<=4 ; i++) { // for this type increment always use condtion less than equal to for( int j=3;j>=i;j--){ System.out.print(" "); } for( int k=1;k<=i;k++){ System.out.print("*"); } System.out.println(); } for( int l=1;l<=4;l++){ for(int m=1;m<=l;m++){ System.out.print(" "); } for(int n=3;n>=l;n--){ System.out.print("*"); } System.out.println(); } } } // top left + bottom left + w/o newline // newline // bottom left + top left + w/o newline
/* pyrmid pattern 1, 2, 3, 4 pattern*/ /* * * ** * *** * ***** * * * * * * * * */ public class a_0031 { public static void main(String[] args) { for( int i=1;i<=5;i++){ for( int j=4; j>=i; j--){ System.out.print(" "); } for( int k=1;k<=i;k++){ System.out.print(" *"); } System.out.println(); } System.out.println("second way for printing the pattern "); for(int l=1;l<=5;l++){ for(int m=5;m>=1;m--){ if(m>l){ System.out.print(" "); } else{ System.out.print(" *"); } } System.out.println(); } } } // ma for type two // always run inner loop ( m ) 5 times ( either print * or space, use print ( not println ) // when M>l is true print space, if false print star in one line // formula // L=1, 4 space 1 star // L=2, 3 space 2 star // L=3, 2 space 3 star // L=4, 1 space 4 star // L=5, 0 space 5 star // l = 1 l =2 l=3 l=4 l=5 // 5 > 1 = " " 5 > 2 = " " 5 > 3 = " " 5 > 4 = " " 5 > 5 = " *" // 4 > 1 = " " 4 > 2 = " " 4 > 3 = " " 4 > 4 = " *" 4 > 5 = " *" // 3 > 1 = " " 3 > 2 = " " 3 > 3 = " *" 3 > 4 = " *" 3 > 5 = " *" // 2 > 1 = " " 2 > 2 = " *" 2 > 3 = " *" 2 > 4 = " *" 2 > 5 = " *" // 1 > 1 = " *" 1 > 2 = " *" 1 > 3 = " *" 1 > 4 = " *" 1 > 5 = " *"
/* a_0032 star pattern 1, 3, 5, 7, pattern*/ /* * * *** * ***** * ******* * ********* */ public class a_0032 { public static void main(String[] args) { for(int i =1;i<=5;i++){ for(int j=4;j>=i;j--){ System.out.print(" "); } for(int k=1;k<=i;k++){ System.out.print("*"); } for( int l=2;l<=i;l++){ System.out.print("*"); } System.out.println(); } System.out.println("second way pattern using 3 for loops"); for( int m=1;m<=5;m++){ for(int n=4;n>=m;n--){ System.out.print(" "); } for(int o=1;o < (m*2);o++){ System.out.print("*"); } System.out.println(); } } } // MA for second type // formula // m=1 , 1 // m=2 , 3 // m=3 , 5 and so on... // for m=1 , always loop o less than m*2 // o < ( m*2 ) // for m=1 // o < ( 1*2 ) = 1<2 - "*" // o < ( 1*2 ) = 2<2 // for m=2 // o < ( 2*2 ) = 1<4 - "*" // o < ( 2*2 ) = 2<4 - "*" // o < ( 2*2 ) = 3<4 - "*" // o < ( 2*2 ) = 4<4 // for m=3 // o < ( 3*2 ) = 1<6 - "*" // o < ( 3*2 ) = 2<6 - "*" // o < ( 3*2 ) = 3<6 - "*" // o < ( 3*2 ) = 4<6 - "*" // o < ( 3*2 ) = 5<6 - "*" // o < ( 3*2 ) = 6<6
/* a_0033 reverse of the previous pattern*/ public class a_0033 { public static void main(String[] args) { for( int i=1;i<=3;i++){ for( int j=1;j<=i;j++){ System.out.print(" "); } for(int k=3;k>=i;k--){ System.out.print("*"); } for( int l=2;l>=i;l--){ System.out.print("*"); } System.err.println(); } System.out.println("second way"); for(int a=1;a<=4;a++){ for(int b=1;b<=a;b++){ System.out.print(" "); } for(int c=5;c>(a*2);c--){ System.out.print("*"); } System.out.println(); } System.out.println("third way"); for(int w=3;w>=1;w--){ for(int x=3;x>w;x--){ // at x=3, no space, then 2 and then 3 space System.out.print(" "); } for(int y=1;y<(w*2);y++){ System.out.print("*"); } System.out.println(); } } } // ma third way // print * if y( which is 1 ) is less than w( which is 6 ) // y < (w*2) // for w=3 // 1<(3*2) = 1 < 6 - * // 2<(3*2) = 2 < 6 - * // 3<(3*2) = 3 < 6 - * // 4<(3*2) = 4 < 6 - * // 5<(3*2) = 5 < 6 - * // 6<(3*2) = 6 < 6
/* a_0034 backward slash star pattern*/ /* * * * * * * * * * */ public class a_0034 { public static void main(String[] args) { for( int i=1;i<=5;i++){ //for row for(int j=1;j<=i;j++){ // for row element in one line if(i>=2 && j<=(i-1)){ System.out.print(" "); } else{ System.out.print("*"); } } System.out.println(); } } } // ma code logic // first make simple pattern // formula i>=2 && j<=(i-1) //i=1 0 space 1 star //i=2 1 space 1 star //i=3 2 space 1 star //i=4 3 space 1 star //i=5 4 space 1 star // i>=2 = start from second row // j<=(i-1) = means many space need ( looping number ) // if i=1 then it means 0 or no space print,( only print star)
/* a_0035 print below pattern*/ /* * * * * * * * * * */ public class a_0035 { public static void main(String[] args) { for(int i=1;i<=5;i++){ for(int j=4;j>=i;j--){ System.out.print(" "); } for( int k=1;k<=i;k++){ if(i>=2 && k>1){ System.out.print(" "); } else{ System.out.print("*"); } } System.out.println(); } } } // Ma // two triangle // left top decrement + right down incremenet // for left top decrement , print space j=4;j>=i;j-- // i>=2 = start second position of i // k>1 = start second position of k
/* a_0036 V pattern */ /* * * * * * * * * * * * * * * * */ public class a_0036 { public static void main(String[] args) { for(int i=5;i>=1;i--){ for(int j=5;j>i;j--){ System.out.print(" "); // simple inc. } for(int k=1;k<(i*2);k++){ // simp dec. if(k>1 && k<(i*2)-1){ System.out.print(" "); // inner space. } else{ System.out.print("*"); // } } System.out.println(); } } } // Ma // left top decrement + pyrimid 1.3.5 pattern // simple pattern print // for space // k>1 = space starts form second position // k<(i*2)-1 = and end at the -1 of last position
/* a_0037 A pattern */ public class a_0037 { public static void main(String[] args) { for(int i=1;i<=5;i++){ for(int k=5;k>=i;k--){ System.out.print(" "); // dec } for(int j=1;j<(i*2);j++){ // simple inc. if(j>1 && j<(i*2)-1){ System.out.print(" "); // inner space } else{ System.out.print("*"); } } System.out.println(); } } } // M a // left down increment + inverted V // print simple, inverted V = j=1 ; j<(i*2) ; j++ // print space // j>1 print space fom second position till // j<((i*2)-1) print till -1 of last position
/* a_0038 cross & box pattern */ /* 0 1 2 3 4 5 * 1 * * * * * * 2 * * * * * * 3 * * * * * * 4 * * * * * * 5 * * * * * */ public class a_0038 { public static void main(String[] args) { for(int i =0;i<5;i++){ for( int j=0;j<5;j++){ // System.out.print("*"); if(i==j || i+j==5-1){ System.out.print("*"); } else{ System.out.print(" "); } } System.out.println(); } } } // Ma // fomrula where i==j || i+j==4
/* a_0039 empty box pattern */ /* * j j j j j * 0 1 2 3 4 5 * i 1 * * * * * * i 2 * * * * * * i 3 * * * * * * i 4 * * * * * * i 5 * * * * * * */ public class a_0039{ public static void main(String[] args) { for(int i=1;i<=5;i++){ for(int j=1;j<=5;j++){ // System.out.print(" * "); if(i>=2 && j>=2 && i<=4 && j<=4){ System.out.print(" "); } else{ System.out.print(" *"); } } System.out.println(); } } } // simple box printing // i>=2, prints space excepts the first column ( x axis ) // j>=2, prints space excepts the first row ( y asis ) // i<=4, prints space excepts the last colomn ( x axis) // j>=4, prints space excepts the last row ( y asis )
/* a_0040 numaric patter 1, 12, 123 and 1, 22, 333 */ public class a_0040 { public static void main(String[] args) { for(int i=1;i<=5;i++){ for( int j=1;j<=i;j++){ System.out.print(j); } System.out.println(); } System.out.println("this is another pattern"); for( int k =1;k<=5;k++){ for( int l=1;l<=k;l++){ System.out.print(k); } System.out.println(); } } } // ma code logic // prints j for first pattern // prints k for second pattern
/* a_0041 number patter 1, 23, 456, 78910*/ public class a_0041 { public static void main(String[] args) { int count=0; for(int i=1;i<=5;i++){ for( int j=1;j<=i;j++){ count=count+1; System.out.print(" "+count); } System.out.println(); } } } // variabel value set to 0 , // increment it everytime loop starts // i=1 j=1 , (0+1) = 1 // i=2 j=1 , (1+1) = 2 // j=1 , (2+1) = 3 // and so on...
/* a_0042 patter 1, 21, 321, 4321..*/ public class a_0042 { public static void main(String[] args) { for( int i=1;i<=5;i++){ for(int j=i;j>=1;j--){ System.out.print(j+" "); } System.out.println(); } } }
/* a_0043 pattern 1, 121, 12321, 1234321, ...*/ public class a_0043 { public static void main(String[] args) { for(int i=1;i<=5;i++){ for( int j =1;j<=i;j++){ System.out.print(j+" "); } for( int k=(i-1);k>=1;k--){ System.out.print(k+" "); } System.out.println(); } } } // make two pattern // first 1, 12, 123, 1234, ... // second 1, 21, 321,
/* a_0044 pattern 54321, 5432, 543, 54, 5*/ public class a_0044 { public static void main(String[] args) { for( int i=1; i<=5;i++){ for( int j=5;j>=i;j--){ System.out.print(j+" "); } System.out.println(); } } }
/* a_0045 vertical number pattern*/ /* 1 * 2 6 * 3 7 10 * 4 8 11 13 * 5 9 12 14 15 * */ public class a_0045 { public static void main(String[] args) { for( int i=1;i<=5;i++){ int no=i; for(int j=1;j<=i;j++){ System.out.print(no+" "); no=no+5-j; } System.out.println(); } } }
/* a_0046 forward reverse number pattern */ /* * 1 2 3 * 6 5 4 * 7 8 9 * 12 11 10 * 13 14 15 */ public class a_0046 { public static void main(String[] args) { int count=0; for( int i=1;i<=6;i++){ if(i%2!=0){ for( int j=1;j<=3;j++){ count=count+1; System.out.print(count+" "); } } else{ int temp=count+1; for( int j=count+3;j>=temp;j--){ count=count+1; System.out.print(j+" "); } } System.out.println(); } } }
00.png
Home PREVIOUS NEXT