HackerRank: Right facing hollow triangle

·

1 min read

Given an integer and a character, print the pattern as indicated in the sample testcases.

Input Format

There are two lines in the input:

  • Number of rows (integer)

  • Character to be used

Constraints

None

Output Format

The given pattern

Sample Input 0

6
^
Sample Output 0

^
^^
^ ^
^  ^
^   ^
^^^^^^
Sample Input 1

4
%
Sample Output 1

%
%%
% %
%%%%
import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to       STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        char ch = sc.next().charAt(0);

        for(int i=0; i<n; i++){
            for(int j=0; j<=i; j++){
                if(i == n-1){
                    System.out.print(ch);
                }else{
                if(j ==0 || j == i){
                    System.out.print(ch);
                }else
                System.out.print(" ");
                }
            }
            System.out.println("");
        }
    }
}