Write a Java program to print W star pattern using for loop.
package Shapes3; import java.util.Scanner; public class WStarPattern1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter W Shape Star Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing W Shape Pattern"); for(int i = 0; i < rows; i++) { printingStars(i + 1); printingSpaces(rows - i - 1); printingStars(rows - i + 1); printingSpaces(2 * i); printingStars(rows - i); printingSpaces(rows - i - 1); printingStars(i + 1); System.out.println(); } } public static void printingStars(int rows) { for (int i = 0; i < rows; ++i) { System.out.print("*"); } } public static void printingSpaces(int rows) { for (int i = 0; i < rows; ++i ) { System.out.print(" "); } } }
This Java example uses the while loop to print the alphabetical W star pattern.
package Shapes3; import java.util.Scanner; public class WStarPattern2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter W Shape Star Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing W Shape Pattern"); int i = 0; while(i < rows) { printingStars(i + 1); printingSpaces(rows - i - 1); printingStars(rows - i + 1); printingSpaces(2 * i); printingStars(rows - i); printingSpaces(rows - i - 1); printingStars(i + 1); System.out.println(); i++; } } public static void printingStars(int rows) { int i = 0; while(i < rows) { System.out.print("*"); ++i; } } public static void printingSpaces(int rows) { int i = 0; while( i < rows ) { System.out.print(" "); ++i; } } }
Enter W Shape Star Pattern Rows = 12
Printing W Shape Pattern
* ************************* *
** ************ *********** **
*** *********** ********** ***
**** ********** ********* ****
***** ********* ******** *****
****** ******** ******* ******
******* ******* ****** *******
******** ****** ***** ********
********* ***** **** *********
********** **** *** **********
*********** *** ** ***********
************** *************
In this Java program, printingStars function allows to enter any character and displays the W pattern of a given character.
package Shapes3; import java.util.Scanner; public class WStarPattern3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Rows = "); int rows = sc.nextInt(); System.out.print("Enter Character = "); char ch = sc.next().charAt(0); System.out.println(""); for(int i = 0; i < rows; i++) { printingStars(i + 1, ch); printingSpaces(rows - i - 1); printingStars(rows - i + 1, ch); printingSpaces(2 * i); printingStars(rows - i, ch); printingSpaces(rows - i - 1); printingStars(i + 1, ch); System.out.println(); } } public static void printingStars(int rows, char ch) { for (int i = 0; i < rows; ++i) { System.out.print(ch); } } public static void printingSpaces(int rows) { for (int i = 0; i < rows; ++i ) { System.out.print(" "); } } }
Enter Rows = 16
Enter Character = $
$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $
$$ $$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$ $$
$$$ $$$$$$$$$$$$$$$ $$$$$$$$$$$$$$ $$$
$$$$ $$$$$$$$$$$$$$ $$$$$$$$$$$$$ $$$$
$$$$$ $$$$$$$$$$$$$ $$$$$$$$$$$$ $$$$$
$$$$$$ $$$$$$$$$$$$ $$$$$$$$$$$ $$$$$$
$$$$$$$ $$$$$$$$$$$ $$$$$$$$$$ $$$$$$$
$$$$$$$$ $$$$$$$$$$ $$$$$$$$$ $$$$$$$$
$$$$$$$$$ $$$$$$$$$ $$$$$$$$ $$$$$$$$$
$$$$$$$$$$ $$$$$$$$ $$$$$$$ $$$$$$$$$$
$$$$$$$$$$$ $$$$$$$ $$$$$$ $$$$$$$$$$$
$$$$$$$$$$$$ $$$$$$ $$$$$ $$$$$$$$$$$$
$$$$$$$$$$$$$ $$$$$ $$$$ $$$$$$$$$$$$$
$$$$$$$$$$$$$$ $$$$ $$$ $$$$$$$$$$$$$$
$$$$$$$$$$$$$$$ $$$ $$ $$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$