H – Right-angled triangle pattern 2

Print right-angled triangle pattern. See example for more details.

Input Format

First line of input contains a single integer N – the size of the triangle.

Constraints

1 <= N <= 50

Output Format

For the given integer, print the right-angled triangle pattern.

InputcopyOutputcopy
51
2 6
3 7 10
4 8 11 13
5 9 12 14 15

Explanation 0

Self Explanatory

Solutions:

import java.util.Scanner;

public class RightAngledTriangle {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int currentNumber = 1;
        for (int i = 1; i <= N; i++) {
            int nextNumber = currentNumber;
            for (int j = 1; j <= i; j++) {
                System.out.print(nextNumber + " ");
                nextNumber += (N - j);
            }
            currentNumber++;
            System.out.println();
        }
        scanner.close();
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top