×

Java Programs

Java Practice

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

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.