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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay