Q:

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

0

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

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now