Here’s a short practice exercise for Julia:
- Write a Julia function that takes two arguments (a and b) and returns the sum of the two arguments.
juliaCopy codefunction sum_two_numbers(a, b)
return a + b
end
println(sum_two_numbers(3, 4)) # should print 7
- Write a Julia program that uses a for loop to print the numbers 1 to 10 to the console.
juliaCopy codefor i in 1:10
println(i)
end
- Write a Julia function that takes a number as an argument and returns “even” if the number is even, and “odd” if the number is odd.
juliaCopy codefunction even_or_odd(number)
if number % 2 == 0
return "even"
else
return "odd"
end
end
println(even_or_odd(4)) # should print "even"
println(even_or_odd(7)) # should print "odd"
- Write a Julia program that reads in a user’s name from the console and then prints a personalized greeting.
juliaCopy codeprint("What is your name? ")
name = readline()
println("Hello, $name!")
- Write a Julia program that calculates the area of a rectangle, given its length and width.
juliaCopy codefunction calculate_rectangle_area(length, width)
return length * width
end
println(calculate_rectangle_area(5, 10)) # should print 50
These exercises should help you get more comfortable with Julia programming language. There are many more advanced topics to explore in Julia, such as linear algebra, machine learning, and data analysis. Keep practicing and exploring!