Home » Java programs » Java basic programs

Java program to print ‘W’ pattern using stars



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.
Submitted by IncludeHelp, on December 07, 2017

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
*    ***********    *
**   *****  ****   **
***  ****    ***  ***
**** ***      ** ****
*******        ******





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.