Table of Contents
A program to print interesting patterns
Mode 4
method
Example
Output
Pattern 5
in conclusion
Home Java javaTutorial Program to print interesting patterns

Program to print interesting patterns

Sep 13, 2023 am 11:41 AM
program Print pattern

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
     }
   }
}
Copy after login

Output

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

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

Output

				*	
			*	*	*	
		*	*	*	*	*	
	*	*	*	*	*	*	*	
*	*	*	*	*	*	*	*	*
Copy after login
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();
      }
   }
}
Copy after login

Output

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

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

Output

		*	
	*	*	*	
*	*	*	*	*	
	*	*	*	
		*	
Copy after login
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();
     }
   }
}
Copy after login

Output

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

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 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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

What should I do if the frame line disappears when printing in Excel? What should I do if the frame line disappears when printing in Excel? Mar 21, 2024 am 09:50 AM

If when opening a file that needs to be printed, we will find that the table frame line has disappeared for some reason in the print preview. When encountering such a situation, we must deal with it in time. If this also appears in your print file If you have questions like this, then join the editor to learn the following course: What should I do if the frame line disappears when printing a table in Excel? 1. Open a file that needs to be printed, as shown in the figure below. 2. Select all required content areas, as shown in the figure below. 3. Right-click the mouse and select the &quot;Format Cells&quot; option, as shown in the figure below. 4. Click the “Border” option at the top of the window, as shown in the figure below. 5. Select the thin solid line pattern in the line style on the left, as shown in the figure below. 6. Select &quot;Outer Border&quot;

Insufficient memory or disk space to repagin or print this document Word error Insufficient memory or disk space to repagin or print this document Word error Feb 19, 2024 pm 07:15 PM

This article will introduce how to solve the problem of insufficient memory or disk space to repage or print the document in Microsoft Word. This error usually occurs when users try to print a Word document. If you encounter a similar error, please refer to the suggestions provided in this article to resolve it. Insufficient memory or disk space to repage or print this document Word error How to resolve the Microsoft Word printing error "There is not enough memory or disk space to repage or print the document." Update Microsoft Office Close memory-hogging applications Change your default printer Start Word in safe mode Rename the NorMal.dotm file Save the Word file as another

4 Ways to Print from iPhone 4 Ways to Print from iPhone Feb 02, 2024 pm 04:10 PM

In this digital world, the need for printed pages has not disappeared. While you might think it's more convenient to save content on your computer and send it directly to the printer, you can do the same thing on your iPhone. With your iPhone's camera, you can take a photo or document, and you can also store the file directly for printing at any time. This way you can quickly and easily materialize the information you need and save it in a paper document. Whether at work or in daily life, iPhone provides you with a portable printing solution. The following post will help you understand everything you need to know if you wish to use your iPhone to print pages on a printer. Print from iPhone: Ask Apple

How to pause printing in Windows 11 How to pause printing in Windows 11 Feb 19, 2024 am 11:50 AM

Printed a large file by mistake? Need to stop or pause printing to save ink and paper? There are many situations where you may need to pause an ongoing print job on your Windows 11 device. How to pause printing in Windows 11? In Windows 11, pausing printing will pause the print job, but it will not cancel the print task. This provides users with more flexible control. There are three ways to do this: Pause printing using the taskbar Pausing printing using Windows Settings Printing using the control panel Now, let’s look at these in detail. 1] Print using taskbar Right-click the print queue notification on the taskbar. Click to open all active printer options. Here, right-click on the print job and select Pause All

Can't print from snipping tool in Windows 11/10 Can't print from snipping tool in Windows 11/10 Feb 19, 2024 am 11:39 AM

If you are unable to print using the Snipping Tool in Windows 11/10, it may be caused by corrupted system files or driver issues. This article will provide you with solutions to this problem. Can't print from Snipping Tool in Windows 11/10 If you can't print from Snipping Tool in Windows 11/10, use these fixes: Restart PC Printer Clear print queue Update printer and graphics driver Fix or reset Snipping Tool Run SFC and DISM Scan uses PowerShell commands to uninstall and reinstall Snipping Tool. let us start. 1] Restart your PC and printer Restarting your PC and printer helps eliminate temporary glitches

How to write a simple countdown program in C++? How to write a simple countdown program in C++? Nov 03, 2023 pm 01:39 PM

C++ is a widely used programming language that is very convenient and practical in writing countdown programs. Countdown program is a common application that can provide us with very precise time calculation and countdown functions. This article will introduce how to use C++ to write a simple countdown program. The key to implementing a countdown program is to use a timer to calculate the passage of time. In C++, we can use the functions in the time.h header file to implement the timer function. The following is the code for a simple countdown program

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

See all articles