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

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





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

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 (2015-2018), Some rights reserved.