Pattern Programs
* * * * * * * * * * * * * * *
class Test { public static void main(String[] a) { for(int i = 1; i<=5; i++) { for(int j = 1; j<=i; j++) { System.out.print("*"); } System.out.println(); } } }
* * * * * * * * * * * * * * *
class Test { public static void main(String[] a) { for(int i = 1; i<=5; i++) { for(int j = 5; j>=i; j--) { System.out.print(" "); } for(int k = 1; k<=i; k++) { System.out.print("* "); } System.out.println(); } } }
A A B A B C A B C D A B C D E
class Test { public static void main(String[] a) { for(char i = 'A'; i<='E'; i++) { for(char j = 'A'; j<=i; j++) { System.out.print(j+" "); } System.out.println(); } } }
1 121 12321 1234321
class Test { public static void main(String[] a) { for(int i = 1; i<=4; i++) { for(int j = 3; j>=i; j--) { System.out.print(" "); } for(int k = 1; k<=i; k++) { System.out.print(k); } for(int m = i-1; m>=1;m--) { System.out.print(m); } System.out.println(); } } }
1 21 123 4321 12345
class Test { public static void main(String[] a) { for(int i = 1; i<=5; i++) { if(i%2==0){ for(int j = 1; j<=i; j++) { System.out.print(j); } } else { for(int j = i; j>=1; j--) { System.out.print(j); } } System.out.println(); } } }
1234321 12321 121 1
class Test { public static void main(String[] a) { for(int i =1; i<=4; i++) { for(int j =1; j < i;j++) { System.out.print(" "); } for(int j = 1; j<=5-i; j++) { System.out.print(j); } for(int k = 4-i;k>=1; k--) { System.out.print(k); } System.out.println(); } } }
* * * * * * * * * * * * *
import java.util.Scanner; class Test { public static void main(String[] a) { Scanner s=new Scanner(System.in); System.out.print("Enter a Num: "); int n = s.nextInt(); for(int i = 1; i<=n; i++) { for(int j = 0; j< n-i; j++) { System.out.print(" "); } for(int k=0; k< (2*i)-1; k++) { System.out.print("*"); } System.out.println(); } for(int i = 1; i< n;i++) { for(int j = 0; j< i; j++) { System.out.print(" "); } for(int k=0; k< 2*(n-i)-1; k++){ System.out.print("*"); } System.out.println(); } } }
0 1 0 0 1 0 1 0 1 0 0 1 0 1 0
class Test { public static void main(String[] a) { for(int i = 0; i<=4; i++) { for(int j = 0; j<=i; j++) { System.out.print((i+j)%2+" "); } System.out.println(); } } }
2 4 6 8 10 12
class Test { public static void main(String[] a) { int k = 1; for(int i = 1; i< 4; i++) { for(int j = 1; j<=i; j++) { System.out.print((2*k)+" "); k++; } System.out.println(); } } }
1 3 3 5 5 5 7 7 7 7
class Test { public static void main(String[] a) { int k = 1; for(int i = 0; i< 4; i++) { for(int j = 0; j<=i; j++) { System.out.print((2*i+1)+" "); } System.out.println(); } } }
1 2 3 4 5 6 7 8 9 10
class Test { public static void main(String[] a) { int k = 1; for(int i = 1; i<= 4; i++) { for(int j = 1; j<=i; j++) { System.out.print(k+" "); k++; } System.out.println(); } } }
1 1 1 1 2 1 1 3 3 1
class Test { int fact(int x) { int res = 1; for(int i = 1; i < x; i++) { res = res*i; } return res; } void show() { int y; for(int i=0; i<=5; i++) { for(int c = 0; c<=(5-i-2); c++) { System.out.print(" "); } for(int c =0; c<= i; c++) { y =fact(i)/(fact(c)*fact(i-c)); System.out.print(y+" "); } System.out.println(); } } public static void main(String[] a) { Test t = new Test(); t.show(); } }