HackerRank: Left facing triangle with odd, ascending numbers

·

1 min read

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

Input Format

A single integer

Constraints

None

Output Format

The given pattern

Sample Input 0

3
Sample Output 0

  1
 13
135
Sample Input 1

5
Sample Output 1

    1
   13
  135
 1357
13579
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();
        for(int i=1; i<= n; i++){
            for(int j = 1; j<= n-i; j++){
                System.out.print(" ");
            }
            for(int k =1; k<=i*2; k +=2){
            System.out.print(k);
            }

            System.out.println("");
        }
    }
}