Wednesday, December 17, 2014

Java Program to print the pattern : 1 21 321 4321 54321



Java Program to print the pattern : 1 21 321 4321 54321


import java.util.Scanner;

public class Pattern 
{
  public static void main(String[] args) 
       {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = scanner.nextInt();

        for (int i = 1; i <= n; i++) 
          {
            for (int j = i; j >= 1; j--) 
            {
                System.out.print(j + " ");
             }
          }

     }
}

Here is a sample output :

Enter a number: 5
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

No comments:

Post a Comment