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

Home » Java programs » Java basic programs

Java program to print pattern of numbers in triangle and reverse trainable form

In this java program, we are implementing a program that will print a trainable in normal and reverse order using numbers.
Submitted by IncludeHelp, on December 05, 2017

Example:

Input:
Enter number of rows: 10
Output:
Here is your pattern....!!!
1 2 3 4 5 6 7 8 9 10 
 2 3 4 5 6 7 8 9 10 
  3 4 5 6 7 8 9 10 
   4 5 6 7 8 9 10 
    5 6 7 8 9 10 
     6 7 8 9 10 
      7 8 9 10 
       8 9 10 
        9 10 
         10 
        9 10 
       8 9 10 
      7 8 9 10 
     6 7 8 9 10 
    5 6 7 8 9 10 
   4 5 6 7 8 9 10 
  3 4 5 6 7 8 9 10 
 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10

Program to print triangle in java

import java.util.Scanner;

public class Pattern14
{
    public static void main(String[] args) 
    {
    	// create scanner object.
        Scanner sc = new Scanner(System.in);
         
        //Taking rows value from the user
        System.out.print("Enter rows here : ");
        int rows = sc.nextInt();
         
        System.out.println("Here is your pattern....!!!");
         
        //Printing the pattern
        for (int i = 1; i <= rows; i++) 
        {
          for (int j = 1; j < i; j++) 
            {
                System.out.print(" ");
            } 
          for (int j = i; j <= rows; j++) 
            { 
                System.out.print(j+" "); 
            }   
            System.out.println(); 
        } 
       
        for (int i = rows-1; i >= 1; i--) 
        {
          for (int j = 1; j < i; j++) 
            {
                System.out.print(" ");
            }  
          for (int j = i; j <= rows; j++)
            {
                System.out.print(j+" ");
            }   
            System.out.println();
        }
        sc.close();
    }
}

Output

Enter rows here : 10
Here is your pattern....!!!
1 2 3 4 5 6 7 8 9 10 
 2 3 4 5 6 7 8 9 10 
  3 4 5 6 7 8 9 10 
   4 5 6 7 8 9 10 
    5 6 7 8 9 10 
     6 7 8 9 10 
      7 8 9 10 
       8 9 10 
        9 10 
         10 
        9 10 
       8 9 10 
      7 8 9 10 
     6 7 8 9 10 
    5 6 7 8 9 10 
   4 5 6 7 8 9 10 
  3 4 5 6 7 8 9 10 
 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 








COMMENTS