search
HomeJavajavaTutorialJava program to create pyramids and patterns

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();
      }
   }
}

Output

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

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();
      }
   }
}

Output

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

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();
      }
   }
}

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

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!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Java程序打开命令提示符并插入命令Java程序打开命令提示符并插入命令Aug 19, 2023 pm 12:29 PM

ThisarticleusesvariousapproachesforselectingthecommandsinsertedintheopenedcommandwindowthroughtheJavacode.Thecommandwindowisopenedbyusing‘cmd’.Here,themethodsofdoingthesamearespecifiedusingJavacode.TheCommandwindowisfirstopenedusingtheJavaprogram.Iti

使用类的概念编写Java程序来计算矩形的面积和周长使用类的概念编写Java程序来计算矩形的面积和周长Sep 03, 2023 am 11:37 AM

Java语言是当今世界上最常用的面向对象编程语言之一。类的概念是面向对象语言中最重要的特性之一。一个类就像一个对象的蓝图。例如,当我们想要建造一座房子时,我们首先创建一份房子的蓝图,换句话说,我们创建一个显示我们将如何建造房子的计划。根据这个计划,我们可以建造许多房子。同样地,使用类,我们可以创建许多对象。类是创建许多对象的蓝图,其中对象是真实世界的实体,如汽车、自行车、笔等。一个类具有所有对象的特征,而对象具有这些特征的值。在本文中,我们将使用类的概念编写一个Java程序,以找到矩形的周长和面

Java程序获取给定文件的大小(以字节、千字节和兆字节为单位)Java程序获取给定文件的大小(以字节、千字节和兆字节为单位)Sep 06, 2023 am 10:13 AM

文件的大小是特定文件在特定存储设备(例如硬盘驱动器)上占用的存储空间量。文件的大小以字节为单位来衡量。在本节中,我们将讨论如何实现一个java程序来获取给定文件的大小(以字节、千字节和兆字节为单位)。字节是数字信息的最小单位。一个字节等于八位。1千字节(KB)=1,024字节1兆字节(MB)=1,024KB千兆字节(GB)=1,024MB和1太字节(TB)=1,024GB。文件的大小通常取决于文件的类型及其包含的数据量。以文本文档为例,文件的大小可能只有几千字节,而高分辨率图像或视频文件的大小可

使用继承的Java程序来计算定期存款(FDs)和定期存款(RDs)的利息使用继承的Java程序来计算定期存款(FDs)和定期存款(RDs)的利息Aug 20, 2023 pm 10:49 PM

继承是一个概念,它允许我们从一个类访问另一个类的属性和行为。被继承方法和成员变量的类被称为超类或父类,而继承这些方法和成员变量的类被称为子类或子类。在Java中,我们使用“extends”关键字来继承一个类。在本文中,我们将讨论使用继承来计算定期存款和定期存款的利息的Java程序。首先,在您的本地机器IDE中创建这四个Java文件-Acnt.java−这个文件将包含一个抽象类‘Acnt’,用于存储账户详情,如利率和金额。它还将具有一个带有参数‘amnt’的抽象方法‘calcIntrst’,用于计

Java程序用于检查TPP学生是否有资格参加面试Java程序用于检查TPP学生是否有资格参加面试Sep 06, 2023 pm 10:33 PM

请考虑下表了解不同公司的资格标准-CGPA的中文翻译为:绩点平均成绩符合条件的公司大于或等于8谷歌、微软、亚马逊、戴尔、英特尔、Wipro大于或等于7教程点、accenture、Infosys、Emicon、Rellins大于或等于6rtCamp、Cyber​​tech、Skybags、Killer、Raymond大于或等于5Patronics、鞋子、NoBrokers让我们进入java程序来检查tpp学生参加面试的资格。方法1:使用ifelseif条件通常,当我们必须检查多个条件时,我们会使用

JAVA程序将罗马数字转换为整数数字JAVA程序将罗马数字转换为整数数字Aug 25, 2023 am 11:41 AM

罗马数字-基于古罗马系统,使用符号来表示数字。这些数字称为罗马数字。符号为I、V、X、L、C、D和M,分别代表1、5、10、50、100、500和1,000。整数-整数就是由正值、负值和零值组成的整数。分数不是整数。这里我们根据整数值设置符号值。每当输入罗马数字作为输入时,我们都会将其划分为单位,然后计算适当的罗马数字。I-1II–2III–3IV–4V–5VI–6...X–10XI–11..XV-15在本文中,我们将了解如何在Java中将罗马数字转换为整数。向您展示一些实例-实例1InputR

Java程序创建金字塔和图案Java程序创建金字塔和图案Sep 05, 2023 pm 03:05 PM

如果有人想在Java编程语言方面打下坚实的基础。然后,有必要了解循环的工作原理。此外,解决金字塔模式问题是增强Java基础知识的最佳方法,因为它包括for和while循环的广泛使用。本文旨在提供一些Java程序,借助Java中可用的不同类型的循环来打印金字塔图案。创建金字塔图案的Java程序我们将通过Java程序打印以下金字塔图案-倒星金字塔星金字塔数字金字塔让我们一一讨论。模式1:倒星金字塔方法声明并初始化一个指定行数的整数“n”。接下来,将空间的初始计数定义为0,将星形的初始计数定义为“n+

Java程序旋转图像Java程序旋转图像Sep 01, 2023 pm 04:25 PM

一个图像文件可以顺时针或逆时针旋转。要旋转图像,需要下载一个随机的图像文件并将其保存在系统的任何文件夹中。此外,需要一个.pdf文件,在打开下载的图像文件后,可以在该特定的.pdf文件中旋转一些角度。对于90度的旋转,新图像的锚点可以帮助我们使用Java中的平移变换执行旋转操作。锚点是任何特定图像的中心。AlgorithmtoRotateanImagebyUsingJavaThe"AffineTransformOp"classisthesimplestwaytorotatea

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.