Staircase : HackerRank Problem Solution

Staircase : HackerRank Problem Solution


C / C++ Implementation:


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() 
{
    int N,i,k,j;
    
    /* get the number from the user */
    scanf("%d",&N);
    
    /* print the pattern of staircase */
    for(i=2;i<=N+1;i++)
        {
            k=0;
         for(j=1;j<=(N-i)+1;j++)
         {
             printf(" ");
         }
        
         while(k!=(((2*i)-1)/2))
            {
             printf("#");
             k++;
         }
         printf("\n");
     }
    return 0;
}

Java Implementation:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for(int i = 2;i<=n+1;i++)
            {
            int k = 0;
            for(int j = 1;j<=(n-i)+1;j++)
                {
                System.out.print(" ");
            }
            
            while(k!=(((2*i)-1)/2))
                {
                System.out.print("#");
                k++;
            }
            System.out.print("\n");
        }
    }
}