C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programs

Java programs to print patterns

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.
Submitted by IncludeHelp, on October 27, 2017

All the given java programs to print patterns 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.

Program 1) Print the following pattern


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

Program 2) Print the following pattern


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 

Program 3) Print the following pattern


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 

Program 4) Print the following pattern


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

Program 5) Print the following pattern


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