How to print patterns in Java | Java Pattern Printing Programs

Here, you will find some of the java programs, which are using to print different combinations of number patterns, programs are using user inputs and nested loops, all programs have their outputs. By IncludeHelp Last updated : April 08, 2023

Java Pattern Printing Programs / Examples

All the given Java Pattern Printing Programs of different combinations of numbers are using user input: where program will ask to enter number of rows (so these programs are dynamic type of, you can print till any number of rows) and they are using nested loops: to print the number patterns in the form of rows and columns.

1) Java Number Pattern Example 1


5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1 

Program

import java.util.Scanner;
 
public class Pattern1
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of rows: ");
         
        int rows = sc.nextInt();
         
        System.out.println("Here is your pattern....!!!");
         
        for (int i = rows; i >= 1; i--) 
        {
            for (int j = i; j >= 1; j--)
            {
                System.out.print(j+" ");
            }
             
            System.out.println();
        }       
        sc.close();
    }
}

Output

Enter number of rows: 5
Here is your pattern....!!!
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1

2) Java Number Pattern Example 2


1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

Program

import java.util.Scanner;
 
public class Pattern2
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of rows: ");
         
        int rows = sc.nextInt();
         
        System.out.println("your pattern is: -");
       
        
        for (int i = 1; i <= rows; i++) 
        {
            for (int j = 1; j <= i; j++)
            {
             System.out.print(j+" ");
            }             
            System.out.println();
        }
        sc.close();
    }
}

Output

Enter number of rows: 5
your pattern is: -
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

3) Java Number Pattern Example 3


5 4 3 2 1 
5 4 3 2 
5 4 3 
5 4 
5 

Program

import java.util.Scanner;

public class Pattern3
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of rows: ");
         
        int rows = sc.nextInt();
         
        System.out.println("Here is your pattern....!!!");
         
        for (int i = 1; i <= rows; i++) 
        { 
            for (int j = rows; j >= i; j--)
            {
                System.out.print(j+" ");
            }
             
            System.out.println();
        }       
        sc.close();
    }
}

Output

Enter number of rows: 5
Here is your pattern....!!!
5 4 3 2 1 
5 4 3 2 
5 4 3 
5 4 
5 

4) Java Number Pattern Example 4


1
10
101
1010
10101

Program

import java.util.Scanner;
 
public class Pattern4
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of rows: ");
         
        int rows = sc.nextInt();
         
        System.out.println("Here is your pattern....!!!");
         
        for (int i = 1; i <= rows; i++) 
        {
            for (int j = 1; j <= i; j++)
            {
                if(j%2 == 0)
                {
                    System.out.print(0);
                }
                else
                {
                    System.out.print(1);
                }
            }    
            System.out.println();
        }
        sc.close();
    }
}

Output

Enter number of rows: 5
Here is your pattern....!!!
1
10
101
1010
10101

5) Java Number Pattern Example 5


0  
1 2  
3 4 5  
6 7 8 9  
10 11 12 13 14  

Program

import java.util.Scanner;

public class Pattern5 
{
 public static void main(String[] args) 
 {
   Scanner sc = new Scanner(System.in);
   System.out.print("Enter number of rows: ");
   int rows = sc.nextInt();
         int k=0;
        System.out.println("Here is your pattern....!!!");
        for (int i = 1; i <= rows; i++)
        {
            for (int j = 1; j <= i; j++)
            	System.out.print(k++ +" ");
            System.out.println(" ");
        }  
    }
}

Output

Enter number of rows: 5
Here is your pattern....!!!
0  
1 2  
3 4 5  
6 7 8 9  
10 11 12 13 14  


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.