Universal Code Snippet
IO.puts "Hello, World!"IO.write "Enter your name: "
name = IO.gets("") |> String.trim()
IO.puts "Hello, #{name}!"age = 20
if age >= 18 do
IO.puts "You are an adult."
else
IO.puts "You are a minor."
end# For loop (Enum.each)
IO.puts "For loop:"
Enum.each(1..5, fn i -> IO.write("#{i} ") end)
IO.puts ""
# While loop (recursion)
IO.puts "While loop:"
defmodule Loop do
def while(j) when j <= 5 do
IO.write("#{j} ")
while(j + 1)
end
def while(_), do: IO.puts ""
end
Loop.while(1)def module Math do
def add(a, b), do: a + b
end
IO.puts "Sum: #{Math.add(10, 20)}"numbers = [10, 20, 30, 40, 50]
Enum.each(Enum.with_index(numbers), fn {v, i} ->
IO.puts "Element #{i}: #{v}"
end)str1 = "Hello"
str2 = " World"
IO.puts "Length: #{String.length(str1)}"
combined = str1 <> str2
IO.puts "Concatenated: #{combined}"
IO.puts "Uppercase: #{String.upcase(combined)}"a = 15.0
b = 4.0
IO.puts "Add: #{a + b}"
IO.puts "Subtract: #{a - b}"
IO.puts "Multiply: #{a * b}"
IO.puts "Divide: #{a / b}"