Pattern programs in java with explanations and examples

Pattern programs in java with explanations and examples. Pattern programs in Java include star, numerals, and symbols for beginners in programming

Java program to print the character or a letter x using star

Java program to print the character or a letter x using a star. Using this program in java we can print the letter or string x in the output as a pattern. 

A pattern is someone or something that is used as a template to create a copy, a design, or an expected activity. A pattern is the paper parts that a seamstress uses to build a dress; a dress pattern is an example of a pattern.

import java.util.*;
class Main
{
public static void main(String args[])
{
int i,j,n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to print the character");
n = sc.nextInt();
for(i=n;i>=1;i--)
{
for(j=i;j<n;j++)
{
System.out.print(" ");
}
for(j=1;j<=(2*i-1);j++)
{
if(j==1 || j==(2*i-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
for(i=2;i<=n;i++)
{
for(j=i;j<n;j++)
{
System.out.print(" ");
}
for(j=1;j<=(2*i-1);j++)
{
if(j==1 || j==(2*i-1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println("");
}
}
}

Output:

Enter the number to print the character
5

 

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

Looping

A loop is used to repeatedly execute a set of statements until a certain condition is met. When showing numbers from 1 to 100, for example, you might wish to set the value of a variable to 1 and display it 100 times, raising its value by 1 with each loop iteration.

Iterations

An Iterator is a collection looping object that can be used with ArrayList and HashSet. It’s termed an “iterator” since looping is referred to as “iterating.” You must import an Iterator from java.util package in order to use it.

What is for loop and if else in python program

The startup of the loop, when we set our counter to a beginning value. Before the loop starts, the initialization statement is executed. The test statement determines whether or not a particular condition is true.

For Loop 

A for-loop is a control flow statement in computer science that specifies iteration and allows code to be executed repeatedly. When the number of iterations is known before entering the loop, for-loops are commonly utilized.

If-Else

If statements in Python handle conditional statements, and we’ve already seen how to use conditional statements in other ways, such as with Python if-else. “if condition” – This is used when one of the conditions is true or false and you need to print the result.

Java program pattern program to triangle using 100 numbers

Java program pattern program to triangle using 100 numbers. This pattern program will print the limited 100 numbers in a triangle shape for beginners to understand the basic pattern programming concepts.

public class Main
{
public static void main(String[] args)
{
int lines=10;
int i=1;
int j;
for(i=1;i<=lines;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(i*j+" ");
}
System.out.println("");
}
}
}

1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
10 20 30 40 50 60 70 80 90 100

Write a program to print the down arrow pattern in java

Write a program to print the down arrow pattern in java. Check out the print down arrow asterisk and number pattern program below in four different ways, each with a sample example and output. We added a compiler at the end of the program so that you can run the following codes.

public class Main
{
public static void main(String[] args)
{
int lines=6;
int i,j;
for(i=1;i<=lines;i++)
{
for(j=1;j<=lines;j++)
{
if(i==j)
System.out.print("*");
else
System.out.print("1");
}
j--;
System.out.print("*");
while(j>=1)
{
if(i==j)
System.out.print("*");
else
System.out.print("1");
j--;
}
System.out.println("");
}
}
}

*11111*11111*
1*1111*1111*1
11*111*111*11
111*11*11*111
1111*1*1*1111
11111***11111

Java pattern programs to print the star in between of the numbers

Java pattern programs to print the star in between of the numbers. This java pattern program will print the star pattern in between of the number pattern. 

public class Main
{
public static void main(String []args)
{
int i,j,lines=5;
for(i=1;i<=lines;i++)
{
for(j=lines;j>=1;j--)
{
if(j!=i)
System.out.print(j);
else
System.out.print("*");
}
System.out.println("");
}

}

}

5432*
543*1
54*21
5*321
*4321

Java program to print stars and number pyramid in a single pattern program

Java program to print stars and number pyramid in a single pattern program. This program will print the number and star together in a single program. It will print the number and star in a single output and also it will print a structure in the program like a pyramid for stars and numbers for outer side of the stars

public class Main
{
public static void main(String[] args)
{
int lines=6;
int i,j,k,l;
int space=0;
for(i=0;i<lines;i++)
{
for(j=1;j<=space;j++)
{
System.out.print(" ");
}
for(j=1;j<=lines;j++)
{
if(j<=(lines-i))
System.out.print(j);
else
System.out.print("*");
}
j--;
while(j>0){
if(j>lines-i)
System.out.print("*");
else
System.out.print(j);
j--;
}
if((lines-i)>10)
space=space+1;
System.out.println("");
}
}
}

123456654321
12345**54321
1234****4321
123******321
12********21
1**********1

Java program to print reverse right-angled triangle of numbers

Java program to print reverse right-angled triangle of numbers. In the Java programming language, build a low-facing pattern application. This software will print the pattern with an asterisk at the bottom. In a pattern program for beginners in the Java programming language, print the asterisk downwards.

public class Main 
{ 
public static void main(String[] args) 
{ 
int rows=5; 
for (int i= rows-1; i>=0 ; i--) 
{ 
for (int j=0; j<=i; j++) 
{ 
System.out.print("*" + " "); 
} 
System.out.println(); 
} 
} 
} 

* * * * *                                                                                                                                                

* * * *                                                                                                                                                  

* * *                                                                                                                                                    

* *                                                                                                                                                      

What is triangle pattern program in java

Each small triangle is the same size as its respective small rectangle. To put it another way, our triangle is exactly half the length of the rectangle. If you wish to measure a triangle, simply multiply its base b by its height h and divide by two.

What is rows and columns in java

Rows are a horizontally aligned set of cells that create consistency. Columns are vertically aligned groups of cells that run from top to bottom. A row in a table or spreadsheet is a horizontal line of data, whereas a column is a vertical line of cells in a chart, table, or spreadsheet. Rows run from left to right across the board. Columns, on the other hand, are oriented from top to bottom.

Pyramid pattern program in java using asterisk

Pyramid pattern program in java using asterisk. Java is used to enforce the right side pattern. Only an asterisk can be printed on the right side of a java application using this programme. In the creation of the Java programming language, the pattern is slightly to the right. A 90° angle can be created by cutting a square corner in half or quartering a circle.

public class Main 
{ 
public static void main(String[] args) 
{ 
int n=5; 
for (int i= 0; i<= n; i++) 
{ 
for (int j=1; j<=n-i; j++) 
{ 
System.out.print(" "); 
} 
for (int k=0;k<=i;k++) 
{ 
System.out.print("*"); 
} 
System.out.println(""); 
} 
} 
} 

Output:

    *                                                                                                                                   

    **                                                                                                                                                     

   ***                                                                                                                                                     

  ****                                                                                                                                                     

 *****                                                                                                                                                     

****** 

Java program to print mirrored right triangle star pattern

Java program to print mirrored right triangle star pattern. In  java programming, the mirror side triangle pattern is used. In java pattern application, this program will print asterisk. In any programming language, this program is often called the reverse pattern program.

public class Main
{ 
public static void main(String args[]) 
{ 
int row=7,i,j,k;
for (i= row; i>= 1; i--) 
{ 
for(j=row; j>i;j--) 
{ 
System.out.print(" "); 
} 
for(k=1;k<=i;k++) 
{ 
System.out.print("*"); 
} 
System.out.println(""); 
} 
} 
} 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 
 

Java program to print the pascal triangle using star

Java program to print the pascal triangle using star. In the Java programming language,the right pascal triangle pattern is used. The asterisk right side pascal triangle will be printed by this application. In the java programming language,the asterisk is used to print this right angle pascal program. Beginners in the field of software development may use the triangle pascal program in Java.

import java.util.Scanner; 
public class Main
{ 
public static void main(String[] args) 
{ 
int i, j, rows; 
Scanner sc = new Scanner(System.in); 
System.out.print("Enter the numbers"); 
rows = sc.nextInt(); 
for (i= 0; i<= rows-1; i++) 
{ 
for (j=0; j<=i; j++) 
{ 
System.out.print("*"+ " "); 
} 
System.out.println(""); 
} 
for (i=rows-1; i>=0; i--) 
{ 
for(j=0; j <= i-1;j++) 
{ 
System.out.print("*"+ " "); 
} 
System.out.println(""); 
} 
} 
} 

Output:

enter the numbers5                                                                                                                                       

*                                                                                                                                                        

* *                                                                                                                                                      

* * *                                                                                                                                                    

* * * *                                                                                                                                                  

* * * * *                                                                                                                                                

* * * *                                                                                                                                                  

* * *                                                                                                                                                    

* *                                                                                                                                                      

Left triangle using star pattern program in java

Left triangle using star pattern program in java. This pattern print asterisk pattern in left side pascal pattern. Exactly pascal program is printed in left side facing in the output for beginners in java language

import java.util.Scanner; 
public class Main
{ 
public static void main(String[] args) 
{ 
int i, j, k, rows; 
Scanner sc = new Scanner(System.in); 
System.out.print("Enter the numbers"); 
rows = sc.nextInt(); 
for (i= 1; i<= rows ; i++) 
{ 
for (j=i; j <rows ;j++) 
{ 
System.out.print(" "); 
} 
for (k=1; k<=i;k++) 
{ 
System.out.print("*"); 
} 
System.out.println(""); 
} 
for (i=rows; i>=1; i--) 
{ 
for(j=i; j<=rows;j++) 
{ 
System.out.print(" "); 
} 
for(k=1; k<i ;k++) 
{ 
System.out.print("*"); 
} 
System.out.println(""); 
} 
} 
} 

Output:

enter the numbers5                                                                                                                                         

    *                                                                                                                                                      

   **                                                                                                                                                      

  ***                                                                                                                                                      

 ****                                                                                                                                                      

*****                                                                                                                                                      

 ****                                                                                                                                                      

  ***                                                                                                                                                      

   **                                                                                                                                                      

    

Java program to print pyramid pattern of numbers

Java program to print pyramid pattern of numbers. In the Java programming language, the pyramid number pattern is used. In Java, this program prints numbers pyramid form. Java programming language, print value beginning of the line print pattern.

public class Main
{ 
public static void main(String[] args) 
{ 
int n = 8; 
for (int i = 0; i < n; i++) 
{ 
int a = 1; 
System.out.printf("%" + (n - i) * 2 + "s", ""); 
for (int j = 0; j <= i; j++) 
{ 
System.out.printf("%4d", a); 
a = a * (i - j) / (j + 1); 
} 
System.out.println(); 
} 
} 
} 

Output:

                   1                                                                                                                                     

                 1   1                                                                                                    

               1   2   1                                                                                                                                 

             1   3   3   1                                                                                                                               

           1   4   6   4   1                                                                                                                             

         1   5  10  10   5   1                                                                                                                           

       1   6  15  20  15   6   1                                                                                                                         

     1   7  21  35  35  21   7   1   

Program to print the diamond pattern program using numbers in java

Program to print the diamond pattern program using numbers in java. In the Java programming language, the diamond number pattern is used. In this Java  program, the number will be printed in a diamond form. For beginners, a diamond pattern   software that prints the number in a diamond shape is available.

public class Main
{ 
public static void main(String[] args) 
{ 
for (int i = 1; i <= 4; i++) 
{ 
int n = 10; 
for (int j = 1; j<= n - i; j++) 
{ 
System.out.print(" "); 
} 
for (int k = i; k >= 1; k--) 
{ 
System.out.print(k); 
} 
for (int l = 2; l <= i; l++) 
{ 
System.out.print(l); 
} 
System.out.println(); 
} 
for (int i = 3; i >= 1; i--) 
{ 
int n = 9; 
for (int j = 0; j<= n - i; j++) 
{ 
System.out.print(" "); 
} 
for (int k = i; k >= 1; k--) 
{ 
System.out.print(k); 
} 
for (int l = 2; l <= i; l++) 
{ 
System.out.print(l); 
} 
System.out.println(); 
} 
} 
}

Output   

            1                                                        

        212                                                                                                                                              

       32123                                                                                                                                             

      4321234                                                                                                                                            

       32123                                                                                                                                             

        212                                                                                                                                              

         1                                                                                                                                               



Java program to print pascal’s number of rows

Java program to print pascal’s number of rows. Pascal number program in a java programming language. This program will print the pascal pattern in numbers in java. pascal program is an important method and pattern in every programming language

public class Main
{ 
public static void main(String[] args) 
{ 
int lines=5;
for (int i = 1; i <= lines; i++) 
{ 
for (int j = 1; j <= i; j++) 
{ 
System.out.print(j+" "); 
} 
System.out.println(); 
}
for (int i = lines-1; i >= 1; i--) 
{ 
for (int j = 1; j <= i; j++) 
{ 
System.out.print(j+" "); 
} 
System.out.println(); 
} 
} 
}

Output:

1                                                                                                                               

1 2                                                                                                                             

1 2 3                                                                                                                           

1 2 3 4                                                                                                                         

1 2 3 4 5                                                                                                                       

1 2 3 4                                                                                                                         

1 2 3                                                                                                                           

1 2                                                                                                                             

1    

Java program to print the triangle pattern program using numbers

Java program to print the triangle pattern program using numbers. Right triangle character in a java programming language. This program will the character in java pattern program. right angle triangle program to print the numbers in triangle shape for beginners to understand the pattern in java

public class Main
{ 
public static void main(String[] args) 
{ 
int i, j, lines=5; 
for (i = 1; i <= lines; i++) 
{ 
for (j = 1; j <= i; j++) 
{ 
System.out.print(j+" "); 
} 
for (j = i-1; j >= 1; j--) 
{ 
System.out.print(j+" "); 
} 
System.out.println(); 
} 
} 
} 

Output:

1                                                                                                                               

1 2 1                                                                                                                           

1 2 3 2 1                                                                                                                       

1 2 3 4 3 2 1                                                                                                                   

1 2 3 4 5 4 3 2 1

Java program to print triangle pattern program using a symbol

Java program to print triangle pattern program using a symbol. In the Java programming language, a triangle pattern is used.Using the numbers in Java, this program will print the pattern.Although printing the pattern program is easy, doing the same thing with symbols is more difficult for beginners in Java

public class Main
{ 
public static void main(String[] args) 
{ 
for (int i = 0; i <= 5; i++) 
{ 
int letters = 40; 
for (int j = 5; j > i; j--) 
{ 
System.out.print(" "); 
} 
for (int k = 0; k <= i; k++) 
{ 
System.out.print((char) (letters+ k) + " "); 
} 
System.out.println(); 
} 
} 
}

Output:                                                                                                                         

    ( )                                                                                                                         

   ( ) *                                                                                                                        

  ( ) * +                                                                                                                       

 ( ) * + ,                                                                                                                      

( ) * + , –  

Java program to print diamond pattern of numbers

Java program to print diamond pattern of numbers. Java Diamond pattern program for beginners to intermediate level java programmers, a diamond pattern program using a number in the java programming language. This program will print a pattern using numbers as the program’s outline. 

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int n = 0;
System.out.print("Enter the Number");
n = scan.nextInt();
diamond(n);
}
private static void diamond(int n)
{
if(n <= 0)
System.out.println("Enter non negative numbers");
int a = 0;
for(int i=1; i <= n; i++) 
{
a = 1;
for(int j = i; j <= n; j++) 
{
System.out.print(" ");
}
for(int k = 1; k <= 2*i-1; k++) 
{
System.out.print(a++);
}
System.out.println();
}
for(int i=n-1; i >= 1; i--) 
{
a = 1;
for(int j=n; j >= i; j--) 
{
System.out.print(" ");
}
for(int k=1; k <= 2*i-1; k++) 
{
System.out.print(a++);
}
System.out.println();
}
}
}

Output:

Enter the Number5                                                                                                               

     1                                                                                                                          

    123                                                                                                                         

   12345                                                                                                                        

  1234567                                                                                                                       

 123456789                                                                                                                      

  1234567                                                                                                                       

   12345                                                                                                                        

    123                                                                                                                         

     1

Java program to print a right angle triangle pattern program using for loop

Java program to print a right angle triangle pattern program using for loop. In the Java programming language, build a basic pattern program. To master any programming language, learn pattern programming. The pattern is printed in the first step of a complex level program written in a programming language. a decorative pattern that is replicated. In needlework and other crafts, a model or pattern is used as a reference.

public class Main
{
public static void main(String args[])
{
int i, j, row=7;
for(i=0; i<row; i++)
{
for(j=0; j<=i; j++)
{
System.out.print('*');
}
System.out.println();
}
}
}
*
* *
* * *
* * * *
* * * * *