Home > Java > javaTutorial > Program to print cabin star pattern

Program to print cabin star pattern

WBOY
Release: 2023-09-02 13:45:10
forward
946 people have browsed it

Solving interesting pattern problems can enhance understanding of loops. They are essential because they help build a solid foundation in a specific programming language. There are various modes, including number-based, asterisk-based, and letter-based modes. This article will guide you to solve a cabin star schema using nested for loops in Java.

Java program to print cabin star pattern

Since we are going to use nested for loops to solve the problem, it is necessary to discuss its syntax.

grammar

for ( initial expression; conditional expression; increment/decrement expression ){
   for ( initial expression; conditional expression; increment/decrement expression ) {
      // code to be executed
   }
}
Copy after login

Initial expression - Executed once at the beginning of the loop.

Conditional Expression - The code will be executed when the conditional expression is true.

Increment/decrement expression - Increment/decrement loop variable.

The Chinese translation of

Pattern

is:

Pattern

Program to print cabin star pattern

method

  • Divide the entire pattern into two parts. The first part is an upper triangular shape and the second part is a lower rectangular part.

  • Declare and initialize an integer "n", specifying the number of lines in the upper and lower parts.

  • Declare and initialize the initial count of spaces and stars.

  • Now, define a nested for loop for the upper triangle part. The outer for loop will run to "n" and the first inner loop will run to the space count and print the spaces. Decrease the number of spaces by 1 after printing.

  • The second inner for loop will run until the stars are counted, and the stars will be printed. Increase the star count by 2 after printing.

  • Create another nested for loop again. The outer for loop will run till 'n', the first inner loop will print the left rectangle shape, the second inner loop will print spaces and the last inner loop will print the right rectangle shape.

Example

public class Hut {
   public static void main(String[] args) {
      // count of upper triangle row and lower rectangle row 
      int n = 5; 
      int spc = n-1; 
      // initial count of space
      int str = 1; 
      // initial count of star
      // upper triangular shape
      for(int i = 1; i <= n; i++) {
         for(int j = 1; j <= spc; j++) {
            // for space
            System.out.print("\t"); 
         }
         spc--;
         for(int k = 1; k <= str; k++) { 
            // for star
            System.out.print("*\t");  
         }
         str += 2;
         System.out.println(); 
         // to move the cursor to next line
      }
      // lower rectangular shape
      for (int i = 0; i < n; i++) {
         // for left rectangular shape
         for (int j = 0; j < n/2; j++) { 
            System.out.print("*\t");
         }
         // for space
         for (int j = 0; j < 5; j++) {
            System.out.print("\t");
         }
         // for right rectangular shape
         for (int j = 0; j < n/2; j++) {
            System.out.print("*\t");
         }
         System.out.println(); 
         // to move the cursor to next line
      }
   }
}
Copy after login

Output

				*	
			*	*	*	
		*	*	*	*	*	
	*	*	*	*	*	*	*	
*	*	*	*	*	*	*	*	*	
*	*						*	*	
*	*						*	*	
*	*						*	*	
*	*						*	*	
*	*						*	*	
Copy after login

in conclusion

In this article, we discussed the solution for Hut Star Schema. We solved this particular problem with the help of nested for loops. This will help you decode the logic of pattern problems and enable you to solve other patterns on your own.

The above is the detailed content of Program to print cabin star pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template