Staircase

See the original problem on HackerRank.

Solutions

This exercise is very easy. At Coding Gym we experiment with such exercises to learn more.

Some solutions follow.

C++:

1
2
3
4
5
int N; cin >> N;
for (auto i=1; i<=N; ++i)
{
    cout << string(N-i, ' ') << string(i, '#') << endl;        
}    

C#:

1
2
3
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= n; i++)
    Console.WriteLine(new String(' ', n - i) + new String('#', i));

Python:

1
2
3
n = input()
for i in range(1,n+1):
    print " "*(n-i) + "#"*i

Ruby:

1
2
3
4
n = gets.strip.to_i
(1..n).each do |i|
    puts( " " * (n-i) + "#"*(i))
end
We've worked on this challenge in these gyms: modena 
comments powered by Disqus