Q:

Java program to print \'W\' pattern using stars

belongs to collection: Java Basic Programs

0

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

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to count factors of a given number... >>
<< Java program to convert string to Boolean...