Pattern Printing β Others
21 October 2024 at 02:01
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: