# HackerRank – Staircase (Java)

## Description

https://www.hackerrank.com/challenges/staircase

Consider a staircase of size n = 4:

   #
##
###
####


Observe that its base and height are both equal to n, and the image is drawn using # symbols and spaces. The last line is not preceded by any spaces.

Write a program that prints a staircase of size n.

Input Format

A single integer, , denoting the size of the staircase.

Output Format

Print a staircase of size using # symbols and spaces.

Note: The last line must have 0 spaces in it.

Sample Input

6


Sample Output

     #
##
###
####
#####
######


Explanation

The staircase is right-aligned, composed of # symbols and spaces, and has a height and width of n = 6.

## Explanation

The staircase is actually printed on a 2D array.

If (row index + column index) > n, print “#”; otherwise, print ” “.

## Java Solution

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

public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if ((i + j) > n) {
System.out.print("#");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

## 3 Thoughts to “HackerRank – Staircase (Java)”

1. xxx says:

if((i+j)>=n || i==n-j-1)
this is the if statement

2. xyz says:

its wrong:-{

1. xxx says:

if((i+j)>=n || i==n-j-1)
this is the correct if statement :))