DEV Community

Pau Riosa
Pau Riosa

Posted on • Edited on • Originally published at paugramming.com

5 4

Elixir Today: Create a Right Triangle Using Elixir

Process

  • create a file named right_triangle.ex
  • write the code
right_triangle = fn number ->
  for i when i <= number <- 0..number do
    # for printing spaces
    spaces =
      for j when j < number - i <- 0..number do
        " "
      end

    # for printing star
    star =
      for _k <- 0..i do
        "*"
      end

    spaces ++ star
  end
  |> Enum.into("", fn string ->
    string = Enum.join(string)
    "#{string}\n"
  end)
end

IO.puts(right_triangle.(10))
IO.puts(right_triangle.(5))

Enter fullscreen mode Exit fullscreen mode
  • run elixir right_triangle.ex

Result

          *
         **
        ***
       ****
      *****
     ******
    *******
   ********
  *********
 **********
***********

     *
    **
   ***
  ****
 *****
******

Enter fullscreen mode Exit fullscreen mode

Happy Coding!

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay