Print Patterns in JAVA : part 2


Levels of difficulty: / perform operation:

Pattern 1

*                 *
* *             * *
* * *         * * *
* * * *     * * * *
* * * * * * * * * *

JAVA Program

public class PatternClass {
	System.out.println("The Pattern is");
	int num=12;
	int f=2;
	int g=num-1;
	for (int i=1;i<=(num/2);i++) {
		for (int j=1;j<=num;j++) {
			if(j>=f&&j<=g) {
				System.out.print(" ");
			} else {
				System.out.print("*");
			}
		}
		f=f+1;
		g=g-1;
		System.out.println();
	}
}

Pattern 2

* * * * * * * * * * * 
  * * * * * * * * * 
    * * * * * * *
      * * * * *
        * * *
          *

JAVA Program

public class PatternClass {
	System.out.println("The Pattern is");
	int num1=11;
	int f1=1;
	int g1=num1;
	for (int i=1;i<=(num1/2)+1;i++) {
		for (int j=1;j<=num1;j++) {
			if((j>=f1&&j<=g1)) {
				System.out.print("*");
			} else {
				System.out.print(" ");
			}
		}
		f1=f1+1;
		g1=g1-1;
		System.out.println();
	}
}

Pattern 3

          * 
        * * * 
      * * * * * 
    * * * * * * *
  * * * * * * * * * 
* * * * * * * * * * *

JAVA Program

public class PatternClass {
	System.out.println("The Pattern is");
	int num2=11;
	int f2=(num2/2)+1;
	int g2=(num2/2)+1;
	for (int i=1;i<=(num2/2)+1;i++) {
		for (int j=1;j<=num2;j++) {
			if((j<=f2&&j>=g2)) {
				System.out.print("*");
			} else {
				System.out.print(" ");
			}
		}
		f2=f2+1;
		g2=g2-1;
		System.out.println();
	}
}

Pattern 4

* * * * *
  * * * *
    * * *
      * *
        *

JAVA Program

public class PatternClass {
	System.out.println("The Pattern is");
	for (int i=1;i<=5;i++) {
		for (int j=1;j<=5;j++) {
			if(j>=i) {
				System.out.print("*");
			} else {
				System.out.print(" ");
			}
		}
		System.out.println();
	}
}

Pattern 5

        *
      * * 
    * * *
  * * * *
* * * * *

JAVA Program

public class PatternClass {
	System.out.println("The Pattern is");
	for (int i=5;i>=1;i--) {
		for (int j=1;j<=5;j++) {
			if(j>=i) {
				System.out.print("*");
			} else {
				System.out.print(" ");
			}
		}
		System.out.println();
	}
}