search
HomeJavajavaTutorialProgram to print interesting patterns

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. In this article, we will discuss some Java programs to print interesting star patterns.

A program to print interesting patterns

Mode 1

Program to print interesting patterns

method

  • Declare and initialize an integer "n" that specifies the number of rows and columns.

  • Define a for loop that will run to "n". Define an if-else block inside this loop.

  • The if block will print the asterisk "n" times in the first and last lines, the else block will print the space "n/2" in the second to fourth lines, which means 2 times and the asterisk No. once.

Example

public class P1 {
   public static void main(String[] args) {
     int n = 5; 
     for(int i = 1; i <= n; i++) { 
       // till number of rows
       if(i == 1 || i == n) { 
         for(int str = 1; str <= n; str++) {
            System.out.print("*\t");
         } 
       } else {
         for(int spc = 1; spc <= n/2; spc++) {
            System.out.print("\t");
         }
         for(int str = 1; str <= 1; str++){
            System.out.print("*");
         } 
       }
       System.out.println();  
       // to move cursor to next line
     }
   }
}

Output

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

Mode 2

Program to print interesting patterns

method

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

  • Create a nested for loop, the outer loop will run to 'n', the inner loop will run to the number of spaces and print the spaces. After printing, we decrement the space count 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 P2 {
   public static void main(String[] args) {
      int n = 5;
      int spc = n-1; // initial space count
      int str = 1; // initial star count
      for(int i = 1; i <= n; i++) {
         for(int j = 1; j <= spc; j++) { // spaces
            System.out.print("\t"); 
         }
         spc--;
         for(int k = 1; k <= str; k++) { // stars
            System.out.print("*\t");  
         }
         str += 2;
         System.out.println();
      }
   }
}

Output

				*	
			*	*	*	
		*	*	*	*	*	
	*	*	*	*	*	*	*	
*	*	*	*	*	*	*	*	*
The Chinese translation of

Pattern 3

is:

Pattern 3

Program to print interesting patterns

method

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

  • Define a nested for loop, the outer loop will run from 'n' to 1, the first inner loop will print spaces, and the second inner loop will print asterisks.

Example

public class P3 {
   public static void main(String[] args) {
      int n=5;
      for(int i = n; i >= 1; i--) {
         for(int spc = n-i; spc >= 1; spc--){ // spaces
            System.out.print("\t");
         }
         for(int str = 1; str <= i; str++){ // stars
            System.out.print("*\t");
         }
         System.out.println();
      }
   }
}

Output

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

Mode 4

Program to print interesting patterns

method

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

  • Create a nested for loop, the outer loop will run to "n", the first inner for loop will run to the number of spaces and print the spaces. The second one will print stars.

  • Now, using the if-else block, the if block will check if the line number is less than 3. If less than then execute the if block to decrement the space count and increment the star count.

  • If the line number is greater than 2, execute the else block to increment the space count and decrement the asterisk count.

Example

public class P4 {
   public static void main(String[] args) {
     int n = 5;
     int spc = 2; 
     // initial space count
     int str = 1; 
     // initial star count
     for (int i = 1; i <= n; i++) {
       for (int j = 1; j <= spc; j++) { 
       // spaces
         System.out.print("\t");
       }
       for (int j = 1; j <= str; j++) { 
       // stars
         System.out.print("*\t");
       }
       if ( i <= 2) { 
         spc--; 
         str += 2;
       } else {
         spc++;
         str -= 2;  
       }
       System.out.println();  
     }
   }
}

Output

		*	
	*	*	*	
*	*	*	*	*	
	*	*	*	
		*	
The Chinese translation of

Pattern 5

is:

Pattern 5

Program to print interesting patterns

method

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

  • Define nested for loops, the outer loop will run to "n", the first inner loop will print spaces, and the second inner loop will print stars.

Example

public class P5 {
   public static void main(String[] args) {
     int n = 5;
     for(int i = 1; i <= n; i++){
       for(int spc = 1; spc <= n-i; spc++) {
         System.out.print("\t");
       }
       for(int str = 1; str <= i; str++) {
         System.out.print("*\t");
       }
       System.out.println();
     }
   }
}

Output

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

in conclusion

In this article, we discuss the interesting problem of star patterns. These pattern solutions 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 interesting 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
ace-guard client exe是什么程序ace-guard client exe是什么程序Sep 22, 2021 pm 06:07 PM

ace-guard client exe是腾讯代理游戏的反作弊程序,是ewido的守护进程,保护“ewido.exe”进程不被恶意软件关闭;使用它可以检测游戏用户是否有开挂行为,可自动进行封号处理。

修复: 操作员拒绝 Windows 任务计划程序中的请求错误修复: 操作员拒绝 Windows 任务计划程序中的请求错误Aug 01, 2023 pm 08:43 PM

要自动化任务和管理多个系统,任务计划软件是您武器库中的宝贵工具,尤其是对于系统管理员而言。Windows任务计划程序完美地完成了这项工作,但最近许多人报告说操作员拒绝了请求错误。该问题存在于操作系统的所有迭代中,即使已经广泛报告和涵盖,也没有有效的解决方案。继续阅读以找到真正对其他人有用的内容!操作员或管理员拒绝了任务计划程序0x800710e0中的请求是什么?任务计划程序允许在没有用户输入的情况下自动执行各种任务和应用程序。您可以使用它来安排和组织特定应用程序、配置自动通知、帮助传递消息等。它

如何在Windows 10和11上按面部对照片进行排序如何在Windows 10和11上按面部对照片进行排序Aug 08, 2023 pm 10:41 PM

Windows的操作随着每个版本而变得越来越好,具有诱人的功能来改善用户体验。用户希望在Windows10和11上探索的一项功能是能够按面部对照片进行排序。此功能允许您通过面部识别对朋友和家人的照片进行分组。听起来很有趣,对吧?继续阅读如何了解如何利用该功能。我可以在Windows上按面孔对照片进行分组吗?是的,您可以使用“照片”应用在Windows10和11上按人脸对图片进行分组。但是,此功能在照片应用程序版本上不可用。此外,您可以使用“人脉”选项卡将这些照片链接到联系人。因此,使用此功能可以

microsoft visual c++可以卸载吗?microsoft visual c++可以卸载吗?Sep 14, 2022 am 11:36 AM

“microsoft visual c++”是可以卸载的,但是不建议卸载;“microsoft visua”这些都是一些微软的组件,里面包括一些“C++”标准库、原始数据库等相关信息,很多软件尤其是游戏中需要“microsoft visual c++”中的环境组件,如果缺少了“C++”标准库的支持,可能会造成软件的无法运行。

如何自动切换特定应用程序的iPhone方向锁定如何自动切换特定应用程序的iPhone方向锁定Jun 06, 2023 am 08:22 AM

在iOS中,当您将iPhone从纵向旋转到横向时,许多App会显示不同的视图。根据应用程序及其使用方式,这种行为并不总是可取的,这就是Apple在“控制中心”中包含方向锁定选项的原因。但是,某些应用程序在禁用方向锁定的情况下工作得更有用-想想YouTube或照片应用程序,将设备旋转到横向可以提供更好的全屏观看体验。如果您倾向于保持锁定状态,则必须在每次打开这些类型的应用程序时在“控制中心”中禁用它以获得全屏体验。然后,当您关闭应用程序时,您必须记住重新打开方向锁定,这并不理想。幸运的是,您可以创

如何从Microsoft商店快速卸载应用如何从Microsoft商店快速卸载应用Jul 12, 2023 pm 09:25 PM

Microsoft应用商店是内置存储库,用户可以在其中下载、更新和卸载适用于Windows操作系统的应用。可悲的是,许多用户不知道如何在MicrosoftStore上卸载应用程序。因此,本文将带您了解如何快速从Microsoft商店卸载应用程序。或者,如果您的Windows11PC上缺少Microsoft应用商店应用程序,我们提供了有关下载和安装应用商店应用程序的详细指南。是否可以直接从Microsoft应用商店卸载应用?否,Microsoft应用商店不提供直接从平台卸载应用的选项。您只能通过平

C语言中的身份矩阵程序C语言中的身份矩阵程序Aug 30, 2023 am 10:45 AM

给定一个方阵M[r][c],其中“r”是一定数量的行,“c”是列,使得r=c,我们必须检查“M”是否是单位矩阵。恒等矩阵恒等矩阵也称为大小为nxn方阵的单位矩阵,其中对角元素的整数值为1,非对角元素的整数值为0就像下面给定的示例-$$I1=\begin{bmatrix}1\end{bmatrix},\I2=\begin{bmatrix}1&0\0&1\end{bmatrix},\I3=\begin{bmatrix}1&0&0\0&1&0\0&

卸载程序的文件名是什么卸载程序的文件名是什么Oct 21, 2022 pm 02:05 PM

卸载程序的文件名是“uninstall.exe”或“uninst.exe”,是用以协助使用者将软件自电脑中删除的一种电脑软件。使用方法:1、在文件资源管理器中挖掘并导航到应用程序EXE文件所在的文件路径;2、通过文件路径打开应用程序的安装目录,找到“uninstall.exe”文件;3、双击卸载文件“uninstall.exe”即可开始程序删除过程。

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

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.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),