Thursday, December 18, 2014

Java Programs to print pattern 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1


Java Programs to print pattern 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1



import java.util.Scanner;

public class Pattern {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = scanner.nextInt();
        int currentNumber = (int) (n * (n + 1) / 2);
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print(currentNumber + " ");
                currentNumber--;
            }
            System.out.println();
        }
    }

}

Output

Enter n: 5
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1

No comments:

Post a Comment