W Pattern Programming Questions in Java

W Pattern Programming Questions in Java

This blog explains about W Pattern Programming Questions in Java . It is explained very well below :

______________________________________________________________________________________

Here, we are implementing a java program that will print a pattern like ‘W’ alphabet using stars. It is a pattern printing program in java.

Given number of rows for pattern and we have to print a pattern like ‘W’ using java program.

Example:

Input:
Enter number of rows: 5
Output:
Pattern is:
*    ***********    *
**   *****  ****   **
***  ****    ***  ***
**** ***      ** ****
*******        ******

Program to print patter like ‘W’ in java

import java.util.Scanner;

public class Pattern23
{
	// create class for printing "*" star.
	private static void stars(int count)
	{
		for (int i = 0; i < count; ++i)
	    System.out.print("*");
	}
	
	// create class for printing " " space.
	private static void spaces(int count)
	{
	    for (int i = 0; i < count; ++i)
	    System.out.print(" ");
	}
	 
	public static void main(String[] args)
	{
		// initialize and create object.
		int n;
		Scanner s=new Scanner(System.in);
	      
	    // enter number of rows.
	    System.out.print("Enter the number for pattern : ");
	    n=s.nextInt();
	    
	    for (int i = 0; i < n; ++i) 
	    {
	        stars(i + 1);
	        spaces(n - i - 1);
	        stars(n - i + 1);
	        spaces(2 * i);
	        stars(n - i);
	        spaces(n - i - 1);
	        stars(i + 1);
	 
	        System.out.println();
	    }
	}
}

Output

Enter the number for pattern : 5

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

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

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

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

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

PROGRAM:

package pattern.aug;

public class Wpattern {

public static void main(String[] args) {
// TODO Auto-generated method stub

//1stline
System.out.print(“*”);
for(int i=0;i<9;i++)
{
System.out.print(” “);
}
System.out.print(“*”);
System.out.println();

//2nd line
System.out.print(” “);
System.out.print(“*”);
for(int i=0;i<2;i++){
for(int j=0;j<3;j++)
{
System.out.print(” “);
}
System.out.print(“*”);
}
System.out.println();

//3rd line
for(int i=0;i<2;i++)
{
System.out.print(” “);
}
for(int i=0;i<4;i++)
{
System.out.print(“*”);
System.out.print(” “);
}
System.out.println();

//4th line
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(” “);
}
System.out.print(“*”);
}
}
}

 ____________________________________________________________________________________

REFERENCES :

https://www.includehelp.com/java-programs/print-w-pattern-using-stars.aspx