❌

Reading view

There are new articles available, click to refresh the page.

Pattern Printing – Others

public class Patterns {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//PRINTING 1's and 0's IN SPECIFIC PATTERN
		int n=5;
		Patterns ptn = new Patterns();
		ptn.pattern1(n);
		System.out.println();
		ptn.pattern2(n);
		System.out.println();
		ptn.pattern3("SUGIRTHA");
	}
	private void pattern1(int n) {
		int val=0;
		for (int r=1; r<=n; r++) {
			val = r%2==0?0:1;
			for (int c=1; c<=n-r+1; c++) {
				System.out.print(" "+val);
				val=1-val;
			}
			System.out.println();
		}
	}
	private void pattern2(int n) {
		int val=1;
		for (int r=1; r<=n; r++) {
			for (int c=1; c<=n-r+1; c++) {
				System.out.print(" "+val);
			}
			val=1-val;
			System.out.println();
		}
	}
	private void pattern3(String name) {
		int n = name.length();
		for (int r=1; r<=n; r++) {
			for (int c=0; c<r; c++) {
				System.out.print(" "+name.charAt(c));  //TBD 
			}
			System.out.println();
		}
	}

}

OUTPUT:

Patterns – Printing Name

public class PatternMyName {
	//  MY NAME - IN PATTERN PRINTING

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PatternMyName pattern = new PatternMyName();
		int n=9;
		pattern.printLtr("S",n);
		pattern.printLtr("U",n);
		pattern.printLtr("G",n);
		pattern.printLtr("I",n);
		pattern.printLtr("R",n);
		pattern.printLtr("T",n);
		pattern.printLtr("H",n);
		pattern.printLtr("A",n);
	}
	private void printLtr(String ltr,int n) {
		int m=n/2 +1;
		switch(ltr) {
			case "S":
				for (int r=1; r<=n; r++) {
					for (int c=1; c<=n; c++) {
						if (r==1 || r==n || r==m || (c==1 && r<m) || (c==9 && r>m)) {
							if ((c==1 && r==1) || (r==n && c==n) || (r==m && (c==1 || c==n))) System.out.print("  ");
							else System.out.print(" *");
						}
						else System.out.print("  ");
					}
					System.out.println();
				}
				System.out.println();
				break;
				
			case "U":
				for (int r=1; r<=n; r++) {
					for (int c=1; c<=n; c++) {
						if ((c==1 && r<n)|| (c>1 && c<n && r==n) || (r<n && c==n))
								System.out.print(" *");
						else System.out.print("  ");
					}
					System.out.println();
				}
				System.out.println();
				break;
			
			case "G":
				for (int r=1; r<=n; r++) {
					for (int c=1; c<=n; c++) {
						if ((r==1 && c!=1) || (c==1 && r!=1 && r!=n) || (r==n && c!=1 && c!=n) || (c==n && r>=m && r!=n) || (r==m && c>m)) System.out.print(" *");
						else System.out.print("  ");
					}
					System.out.println();
				}
				System.out.println();
				break;

			case "I":
				for (int r=1; r<=n; r++) {
					for (int c=1; c<=n; c++) {
						if ( c==m || r==1 || r==n) System.out.print(" *");
						else System.out.print("  ");
					}
					System.out.println();
				}
				System.out.println();
				break;
				
			case "R":
				for (int r=1; r<=n; r++) {
					for (int c=1; c<=n; c++) {
						if ( (c==1 && r!=1) || (r==1 && c!=n && c!=1) || (c==n && r<m && r!=1) || (r==m && c!=n) || (r>m && c==m+(r-m))) System.out.print(" *");
						else System.out.print("  ");
					}
					System.out.println();
				}
				System.out.println();
				break;
				
			case "T":
				for (int r=1; r<=n; r++) {
					for (int c=1; c<=n; c++) {
						if ( c==m || r==1 ) System.out.print(" *");
						else System.out.print("  ");
					}
					System.out.println();
				}
				System.out.println();
				break;
				
			case "H":
				for (int r=1; r<=n; r++) {
					for (int c=1; c<=n; c++) {
						if ( c==1 || c==n || r==m) System.out.print(" *");
						else System.out.print("  ");
					}
					System.out.println();
				}
				System.out.println();
				break;
			
			case "A":
				for (int r=1; r<=n; r++) {
					for (int c=1; c<=n; c++) {
						if ( (r<=m && (c==m-r+1 || c==m+r-1)) || r==m || (r>m && (c==1 || c==n))) System.out.print(" *");
						else System.out.print("  ");
					}
					System.out.println();
				}
				System.out.println();
				break;

		}
	}
}

OUTPUT:

❌