DMCA.com Protection Status
 
Search

How to Print Pattern in Java || Java Programs

Java pattern program enhances the coding skill, logic, and looping concepts. It is mostly asked in Java interview to check the logic and thinking of the programmer. We can print a Java pattern program in different designs. To learn the pattern program, we must have a deep knowledge of the Java loop, such as for loop do-while loop. In this section, we will learn how to print a pattern in Java.


We have classified the Java pattern program into three categories:

  • Start Pattern

  • Number Pattern

  • Character Pattern

Star Pattern


1. Right Triangle Star Pattern

public class RightTrianglePattern   
{   
public static void main(String args[])   
{   
//i for rows and j for columns     
//row denotes the number of rows you want to print 
int i1, j1, row=6;   
//outer loop for rows 
for(i1=0; i1<row; i1++)   
{   
//inner loop for columns 
for(j1=0; j1<=i1; j1++)   
{   
//prints stars  
System.out.print("* ");   
}   
//throws the cursor in a new line after printing each line 
System.out.println();   
}   
}   
}  

Output -


*

**

***

****

*****

******


Left Triangle Star Pattern

public class LeftTrianglePattern  
{    
public static void main(String args[])   
{    
//i for rows and j for columns     
//row denotes the number of rows you want to print 
int i, j, row = 6;       
//Outer loop work for rows 
for (i=0; i<row; i++)   
{  
//inner loop work for space     
for (j=2*(row-i); j>=0; j--)         
{  
//prints space between two stars     
System.out.print(" ");   
}   
//inner loop for columns 
for (j=0; j<=i; j++ )   
{   
//prints star     
System.out.print("* ");   
}   
//throws the cursor in a new line after printing each line 
System.out.println();   
}   
}   
}  
















7 views0 comments

Related Posts

See All

How to Generate Random Number in Java || Java Programs

In real world projects, we often required to generate random numbers while we develop applications. Many applications have the feature to generate numbers randomly, such as to verify the user many app

Armstrong Number in Java || Java Programs

Armstrong Number in Java: A positive number is called armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc. Let's try to understand why 153 is an

Factorial Program in Java || Java Programs

Factorial Program in Java: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example: 4! = 4*3*2*1 = 24 5! = 5*4*3*2*1 = 120 Factorial Program us

  • Facebook
  • Instagram
  • LinkedIn
  • 1024px-Telegram_logo.svg
  • download
  • YouTube
  • Google Play