Home > Java > javaTutorial > body text

Java program to create pyramids and patterns

WBOY
Release: 2023-09-05 15:05:10
forward
916 people have browsed it

If anyone wants to get a solid foundation in Java programming language. Then, it is necessary to understand how the loop works. Furthermore, solving Pyramid Pattern problems is the best way to enhance your knowledge of Java fundamentals as it includes extensive use of for and while loops. This article aims to provide some Java programs to print pyramid patterns with the help of different types of loops available in Java.

Java program to create pyramid pattern

We will print the following pyramid pattern through Java program -

  • Inverted Star Pyramid

  • 星pyramid

  • Number Pyramid

Let’s discuss them one by one.

Mode 1: Inverted Star Pyramid

Java program to create pyramids and patterns

method

  • Declares and initializes an integer "n" specifying the number of rows.

  • Next, define the initial count of the space as 0 and the initial count of the star as "n n – 1" so that we can keep the number of columns to an odd number.

  • Create a nested for loop, the outer loop will run to "n" and the first inner for loop will print a space. After printing, we will increase the space count by 1 on each iteration.

  • Use another inner for loop again to print the stars. After printing, we will subtract 2 from the number of stars.

Example

public class Pyramid1 {
   public static void main(String[] args) {
      int n = 5;
      int spc = 0; // initial space count
      int str = n + n - 1; // initial star count
      // loop to print the star pyramid
      for(int i = 1; i <= n; i++) {
         for(int j = 1; j <= spc; j++) { // spaces
            System.out.print("\t"); 
         }
         spc++; // incrementing spaces
         for(int k = 1; k <= str; k++) { // stars
            System.out.print("*\t");  
         }
         str -= 2; // decrementing stars
         System.out.println();
      }
   }
}
Copy after login

Output

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

Pattern 2: Star Pyramid

Java program to create pyramids and patterns

method

  • Declares and initializes an integer "n" specifying the number of rows.

  • Create a nested for loop, the outer for loop will run to "n" and the inner for loop will run to the number of spaces and print the spaces. After printing, we decrement the number of spaces by 1.

  • Again takes another inner for loop that will run to the number of stars and print the stars. After printing, we increase the star count by 2.

Example

public class Pyramid2 {
   public static void main(String[] args) {
      int n = 5; // number of rows
      int spc = n-1; // initial space count
      int str = 1; // initial star count
      // loop to print the pyramid
      for(int i = 1; i <= n; i++) {
         for(int j = 1; j <= spc; j++) { // spaces
            System.out.print("\t"); 
         }
         spc--; // decrementing spaces
         for(int k = 1; k <= str; k++) { // stars
            System.out.print("*\t");  
         }
         str += 2; // incrementing stars
         System.out.println();
      }
   }
}
Copy after login

Output

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

Mode 3: Number Pyramid

Java program to create pyramids and patterns

method

We'll use the previous code here, but instead of printing stars, we'll print the column number in each row.

Example

public class Pyramid3 {
   public static void main(String[] args) {
      int n = 5; // number of rows
      int spc = n-1; // initial space count
      int col = 1; // initial column count
      // loop to print the pyramid
      for(int i = 1; i <= n; i++) {
         for(int j = 1; j <= spc; j++) { // spaces
            System.out.print("\t"); 
         }
         spc--; // decrementing spaces
         for(int k = 1; k <= col; k++) { // numbers
            System.out.print(k + "\t");  
         }
         col += 2; // incrementing the column
         System.out.println();
      }
   }
}
Copy after login

Output

            1	
	 1  2  3	
      1	 2  3  4  5	
   1  2  3  4  5  6  7	
1  2  3  4  5  6  7  8  9
Copy after login

in conclusion

In this article, we discussed three Java programs for printing pyramid patterns. These pattern solutions will help us decode the logic of pattern problems and enable us to solve other patterns on our own. To solve such patterns, we use loops and conditional blocks.

The above is the detailed content of Java program to create pyramids and patterns. 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