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

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.