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 





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.