C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programs » Java basic programs

Java program to print a rectangle using stars (java pattern program)

In this java program, we are going to learn how to print a stars pattern to make a rectangle by using stars?
Submitted by IncludeHelp, on November 03, 2017

There are some other programs, to print patterns of numbers, I would recommend reading them also, and here is the link: Java number patterns programs, print patterns of stars in java

This program will be used to print a star pattern that will generate a rectangle.

Example:

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

Program to print rentable using stars in java

public class Pattern9 
{
	public static void main(String[] args) 
	{
		int number = 7;

		for (int i = 0; i < number; i++) 
		{
			if (i == 0 || i == 6) 
			{
				for (int j = 0; j < number; j++) 
				{
					System.out.print("*");
				}
				System.out.println();
			}
			if (i >= 1 && i <= 5) 
			{
				for (int j = 0; j < number; j++) 
				{
					if (j == 0 || j == 6) 
					{
						System.out.print("*");
					} 
					else if (j >= 1 && j <= 5) 
					{
						System.out.print(" ");
					}
				}
				System.out.println();
			}
		}
	}
}

Output

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




Sponsored Links




COMMENTS