ASSIGNMENT-1 #1.To display a message “The beautiful World” on Julia Script. #CODE println("The beautiful World") OUTPUT The beautiful World #2. Write a program to execute various Arithmetic operators. a = 10 b = 5 println("Addition: ", a + b) println("Subtraction: ", a - b) println("Multiplication: ", a * b) println("Division: ", a / b) println("Modulus: ", a % b) println("Exponentiation: ", a^b) println("Floor Division: ", floor(a / b)) OUTPUT Addition: 15 Subtraction: 5 Multiplication: 50 Division: 2.0 Modulus: 0 Exponentiation: 100000 Floor Division: 2.0 # 3.WAP to show the single and multi-line comments in Julia. # This is a single-line comment println("This is a test") # Inline comment """ This is a multi-line comment. It spans multiple lines. It is enclosed in triple quotes. """ OUTPUT This is a test #4. WAP to show the working of Mod and Floor functions in Julia. println("Modulus: ", 17 % 5) # Mod function println("Floor division: ", floor(17 / 5)) # Floor function OUTPUT Modulus: 2 Floor division: 3.0 #5. WAP to calculate the sum and average of 5 numbers (Entered by user). println("Enter 5 numbers:") num1 = parse(Int, readline()) num2 = parse(Int, readline()) num3 = parse(Int, readline()) num4 = parse(Int, readline()) num5 = parse(Int, readline()) sum = num1 + num2 + num3 + num4 + num5 average = sum / 5 println("Sum: ", sum) println("Average: ", average) OUTPUT Enter 5 numbers: 10 20 30 40 50 Sum: 150 Average: 30.0 # 6.WAP to find out the length of a word entered by user. println("Enter a word:") word = readline() println("Length of the word: ", length(word)) OUTPUT Enter a word: Hello Length of the word: 5 # 7.Check whether the triangle is equilateral, scalene, or isosceles. println("Enter the 3 sides of the triangle:") side1 = parse(Int, readline()) side2 = parse(Int, readline()) side3 = parse(Int, readline()) if side1 == side2 && side2 == side3 println("Equilateral Triangle") elseif side1 == side2 || side2 == side3 || side1 == side3 println("Isosceles Triangle") else println("Scalene Triangle") end OUTPUT Enter the 3 sides of the triangle: 5 5 5 Equilateral Triangle # 8. Write a program to check whether entered character is a vowel or consonant. println("Enter a character:") char = readline() if char in ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"] println("Vowel") else println("Consonant") end OUTPUT Enter the 3 sides of the triangle: 5 5 5 Equilateral Triangle # 9. Write a program to check whether a number is a perfect number or not. println("Enter a number:") num = parse(Int, readline()) sum_divisors = 0 for i in 1:num-1 if num % i == 0 sum_divisors += i end end if sum_divisors == num println("Perfect Number") else println("Not a Perfect Number") end OUTPUT Enter a number: 6 Perfect Number # 10.Write a program to calculate the electricity bill (accept number of units from user). println("Enter number of units consumed:") units = parse(Int, readline()) bill = 0 if units <= 100 bill = 0 elseif units <= 200 bill = (units - 100) * 5 else bill = 100 * 5 + (units - 200) * 10 end println("Electricity Bill: Rs ", bill) OUTPUT:- Enter number of units consumed: 350 Electricity Bill: Rs 2000 # 11.WAP to check whether the last digit of a number (entered by user) is divisible by 3 and 2 or not. println("Enter a number:") num = parse(Int, readline()) last_digit = num % 10 if last_digit % 2 == 0 && last_digit % 3 == 0 println("Last digit is divisible by both 2 and 3") else println("Last digit is not divisible by both 2 and 3") end OUTPUT Enter a number: 123 Last digit is divisible by both 2 and 3 #12. WAP to accept the cost price of a bike and display the road tax to be paid. println("Enter the cost price of the bike:") cost_price = parse(Float64, readline()) if cost_price > 100000 tax = cost_price * 0.15 elseif cost_price > 50000 && cost_price <= 100000 tax = cost_price * 0.10 else tax = cost_price * 0.05 end println("Road tax to be paid: Rs ", tax) OUTPUT: Enter the cost price of the bike: 120000 Road tax to be paid: Rs 18000.0 #13. Accept the temperature in degree Celsius of water and check whether it is boiling or not. println("Enter the temperature of water in Celsius:") temp = parse(Float64, readline()) if temp >= 100 println("Water is boiling") else println("Water is not boiling") end OUTPUT: Enter the temperature of water in Celsius: 105 Water is boiling #14. Write a program to find out the second maximum number among four numbers. println("Enter four numbers:") a = parse(Int, readline()) b = parse(Int, readline()) c = parse(Int, readline()) d = parse(Int, readline()) numbers = [a, b, c, d] sorted_numbers = sort(numbers) println("Second maximum number: ", sorted_numbers[3]) OUTPUT: Enter four numbers: 10 20 30 40 Second maximum number: 30 #15. WAP to read two arrays data simultaneously in Julia. println("Enter 3 elements for the first array:") array1 = [parse(Int, readline()) for _ in 1:3] println("Enter 3 elements for the second array:") array2 = [parse(Int, readline()) for _ in 1:3] println("First Array: ", array1) println("Second Array: ", array2) OUTPUT: Enter 3 elements for the first array: 1 2 3 Enter 3 elements for the second array: 4 5 6 First Array: [1, 2, 3] Second Array: [4, 5, 6] # 16. WAP to accept age, sex ('M', 'F'), number of days, and display wages accordingly. println("Enter age:") age = parse(Int, readline()) println("Enter sex (M/F):") sex = readline() println("Enter number of days worked:") days = parse(Int, readline()) wage_per_day = 0 if age >= 18 && age < 30 if sex == "M" wage_per_day = 700 elseif sex == "F" wage_per_day = 750 end elseif age >= 30 && age <= 40 if sex == "M" wage_per_day = 800 elseif sex == "F" wage_per_day = 850 end end total_wage = wage_per_day * days println("Total wages: Rs ", total_wage) OUTPUT:Enter age: 25 Enter sex (M/F): M Enter number of days worked: 20 Total wages: Rs 14000 #17. Write a Julia program that accepts a word from the user and reverse it. println("Enter a word:") word = readline() reversed_word = reverse(word) println("Reversed word: ", reversed_word) OUTPUT: Enter a word: hello Reversed word: olleh # 18. Write a Julia program that prints all the numbers from 0 to 6 except 3 and 6. for i in 0:6 if i == 3 || i == 6 continue end println(i) end OUTPUT: 0 1 2 4 5 #19. Write a Julia program which iterates the integers from 1 to 50. # For multiples of three print "Fizz" and for the multiples of five print "Buzz". # For numbers which are multiples of both three and five print "FizzBuzz". for i in 1:50 if i % 3 == 0 && i % 5 == 0 println("FizzBuzz") elseif i % 3 == 0 println("Fizz") elseif i % 5 == 0 println("Buzz") else println(i) end end OUTPUT: 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz # 20.Write a Julia program to guess a number between 1 to 10. # User is prompted to enter a guess. If the user guesses wrong, the prompt appears again. # On successful guess, user will get a "Well guessed!" message. target_number = 7 guess = 0 while guess != target_number println("Guess a number between 1 and 10:") guess = parse(Int, readline()) end println("Well guessed!") OUTPUT: Guess a number between 1 and 10: 3 Guess a number between 1 and 10: 5 Guess a number between 1 and 10: 7 Well guessed! # 21. Write a Julia program that accepts a sequence of lines input and prints the lines as output (all characters in lower case). println("Enter lines (type 'exit' to stop):") while true line = readline() if line == "exit" break end println(lowercase(line)) end OUTPUT: Enter lines (type 'exit' to stop): Hello hello How are you? how are you? exit #22. Write a Julia program which accepts a sequence of comma-separated 4-digit binary numbers and print the numbers that are divisible by 5 in a comma-separated sequence. println("Enter binary numbers (comma separated):") input = readline() binary_numbers = split(input, ",") divisible_by_5 = [] for binary in binary_numbers decimal = parse(Int, binary, base=2) if decimal % 5 == 0 push!(divisible_by_5, binary) end end println("Numbers divisible by 5: ", join(divisible_by_5, ", ")) OUTPUT: Enter binary numbers (comma separated): 1010,1100,1111,1001,10101 Numbers divisible by 5: 1010, 1001 #23. Write a Julia program that accepts a string and calculate the number of digits and letters. println("Enter a string:") input_string = readline() letters = count(c -> isletter(c), input_string) digits = count(c -> isdigit(c), input_string) println("Letters: ", letters) println("Digits: ", digits) OUTPUT: Enter a string: Julia 1.8.5 Letters: 6 Digits: 2 # 24. Write a Julia program to check the validity of password input by users. # Validation: # - At least 1 letter between [a-z] and 1 letter between [A-Z]. # - At least 1 number between [0-9]. # - At least 1 character from [$#@]. # - Minimum length 6 characters. # - Maximum length 16 characters. println("Enter password:") password = readline() valid = length(password) >= 6 && length(password) <= 16 && any(c -> isletter(c), password) && any(c -> isdigit(c), password) && any(c -> c in ['$','#','@'], password) if valid println("Password is valid") else println("Password is invalid") end OUTPUT: Enter password: Hello123@ Password is valid # 25.Write a Julia program to find numbers between 100 and 400 where each digit of a number is an even number. even_numbers = [] for num in 100:400 digits = string(num) if all(c -> parse(Int, string(c)) % 2 == 0, digits) push!(even_numbers, num) end end println("Numbers where each digit is even: ", join(even_numbers, ", ")) OUTPUT Numbers where each digit is even: 200, 204, 206, 208, 220, 222, 224, 226, 228, 240, 242, 244, 246, 248, 260, 262, 264, 266, 268, 280, 282, 284, 286, 288 26.IMPLEMENT STRING OPERATIONS IN JULIA # i. String Concatenation str1 = "Hello" str2 = "World" println(str1 * " " * str2) # ii. String Slicing println(str1[2:4]) # Extracting "ell" # iii. String Interpolation name = "Alice" println("Hello, $name!") # iv. String Repetition println(str1 * 3) # Repeats "Hello" 3 times # v. The Chop Function println(chop("Hello!")) # vi. String Comparison println("apple" == "apple") # true # vii. Splitting String split_string = split("a,b,c,d", ",") println(split_string) # viii. Joining Strings joined_string = join(["a", "b", "c"], ", ") println(joined_string) OUTPUT: Hello World ell Hello, Alice! HelloHelloHello Hello true ["a", "b", "c", "d"] a, b, c # 27. WAP to get 20 inputs by user and create an array of even and odd numbers separately by checking those numbers at runtime. even_numbers = [] odd_numbers = [] println("Enter 20 numbers:") for i in 1:20 num = parse(Int, readline()) # Read input from the user if num % 2 == 0 push!(even_numbers, num) # Add to even array if the number is even else push!(odd_numbers, num) # Add to odd array if the number is odd end end println("Even numbers: ", even_numbers) println("Odd numbers: ", odd_numbers) OUTPUT: Enter 20 numbers: 12 Enter 20 numbers: 25 Enter 20 numbers: 30 Enter 20 numbers: 7 Enter 20 numbers: 18 Enter 20 numbers: 9 Enter 20 numbers: 22 Enter 20 numbers: 3 Enter 20 numbers: 10 Enter 20 numbers: 17 Enter 20 numbers: 44 Enter 20 numbers: 15 Enter 20 numbers: 48 Enter 20 numbers: 5 Enter 20 numbers: 8 Enter 20 numbers: 13 Enter 20 numbers: 50 Enter 20 numbers: 11 Enter 20 numbers: 20 Even numbers: [12, 30, 18, 22, 10, 44, 48, 8, 50, 20] Odd numbers: [25, 7, 9, 3, 17, 15, 5, 13, 11] --------------*************--------------- ASSIGNMENT:02 # 1. Write a Julia function to find the maximum of three numbers. function max_of_three(a, b, c) return max(a, max(b, c)) end # Test the function println("Maximum of 3, 5, 7: ", max_of_three(3, 5, 7)) # OUTPUT: # Maximum of 3, 5, 7: 7 # 2. Write a Julia function to sum all the numbers in an array. function sum_array(arr) return sum(arr) end # Test the function println("Sum of the array [1, 2, 3, 4]: ", sum_array([1, 2, 3, 4])) # OUTPUT: # Sum of the array [1, 2, 3, 4]: 10 # 3. Write a Julia function to implement basic operations on array using map(). function array_operations(arr) addition = map(x -> x + 2, arr) # Add 2 to each element subtraction = map(x -> x - 1, arr) # Subtract 1 from each element multiplication = map(x -> x * 3, arr) # Multiply each element by 3 return addition, subtraction, multiplication end # Test the function arr = [1, 2, 3, 4] addition, subtraction, multiplication = array_operations(arr) println("Addition by 2: ", addition) println("Subtraction by 1: ", subtraction) println("Multiplication by 3: ", multiplication) # OUTPUT: # Addition by 2: [3, 4, 5, 6] # Subtraction by 1: [0, 1, 2, 3] # Multiplication by 3: [3, 6, 9, 12] # 4. Write a Julia function to implement the use of dot (.) operator in array. function dot_operator_example(arr1, arr2) return arr1 .+ arr2 # Element-wise addition end # Test the function arr1 = [1, 2, 3] arr2 = [4, 5, 6] println("Element-wise addition: ", dot_operator_example(arr1, arr2)) # OUTPUT: # Element-wise addition: [5, 7, 9] # 5. Write a Julia function to implement some Math functions like: # div() - Division # fld() - Floor division # cld() - Ceiling division # gcd() - Greatest common divisor # lcm() - Least common multiple function math_operations(a, b) div_res = div(a, b) fld_res = floor(a / b) cld_res = ceil(a / b) gcd_res = gcd(a, b) lcm_res = lcm(a, b) return div_res, fld_res, cld_res, gcd_res, lcm_res end # Test the function a, b = 20, 7 div_res, fld_res, cld_res, gcd_res, lcm_res = math_operations(a, b) println("Division: ", div_res) println("Floor division: ", fld_res) println("Ceiling division: ", cld_res) println("GCD: ", gcd_res) println("LCM: ", lcm_res) # OUTPUT: # Division: 2 # Floor division: 2.0 # Ceiling division: 3.0 # GCD: 1 # LCM: 140 # 6. Write a Julia function to check whether a number falls within a given range (3-6). function check_range(num) return num >= 3 && num <= 6 end # Test the function println("Is 5 within the range 3-6? ", check_range(5)) println("Is 2 within the range 3-6? ", check_range(2)) # OUTPUT: # Is 5 within the range 3-6? true # Is 2 within the range 3-6? false # 7. Write a Julia function that takes a number as a parameter and checks whether the number is prime or not. function is_prime(num) if num <= 1 return false end for i in 2:floor(sqrt(num)) if num % i == 0 return false end end return true end # Test the function println("Is 7 prime? ", is_prime(7)) println("Is 4 prime? ", is_prime(4)) # OUTPUT: # Is 7 prime? true # Is 4 prime? false # 8. Write a Julia function to check whether a string is a pangram or not. function is_pangram(str) str = lowercase(str) return all(c in str for c in 'a':'z') end # Test the function println("Is 'The quick brown fox jumps over the lazy dog' a pangram? ", is_pangram("The quick brown fox jumps over the lazy dog")) # OUTPUT: # Is 'The quick brown fox jumps over the lazy dog' a pangram? true # 9. Write a Julia function that accepts a string and counts the number of upper and lower case letters. function count_case(str) upper_case = count(c -> isuppercase(c), str) lower_case = count(c -> islowercase(c), str) return upper_case, lower_case end # Test the function upper, lower = count_case("The quick Brow Fox") println("No. of Upper case characters: ", upper) println("No. of Lower case Characters: ", lower) # OUTPUT: # No. of Upper case characters: 3 # No. of Lower case Characters: 12 # 10. Write a Julia function that takes an array and returns a new array with distinct elements from the first array. function unique_elements(arr) return unique(arr) end # Test the function println("Unique List: ", unique_elements([1, 2, 3, 3, 3, 3, 4, 5])) # OUTPUT: # Unique List: [1, 2, 3, 4, 5] # 11. Write a Julia program to print the even numbers from a given array. function print_even_numbers(arr) return filter(x -> x % 2 == 0, arr) end # Test the function println("Even numbers: ", print_even_numbers([1, 2, 3, 4, 5, 6, 7, 8, 9])) # OUTPUT: # Even numbers: [2, 4, 6, 8] # 12. Write a Julia function to check whether a number is "Perfect" or not. function is_perfect(num) divisors = [i for i in 1:num-1 if num % i == 0] return sum(divisors) == num end # Test the function println("Is 6 perfect? ", is_perfect(6)) println("Is 28 perfect? ", is_perfect(28)) println("Is 12 perfect? ", is_perfect(12)) # OUTPUT: # Is 6 perfect? true # Is 28 perfect? true # Is 12 perfect? false # 13. Write a Julia program to access a function inside a function. function outer_function(x) println("Inside outer function") function inner_function(y) println("Inside inner function") return x + y end return inner_function(5) end # Test the function println("Result: ", outer_function(10)) # OUTPUT: # Inside outer function # Inside inner function # Result: 15 # 14. Write a Julia function to calculate the area of a parallelogram. # A parallelogram is a quadrilateral with opposite sides parallel (and therefore opposite angles equal). # Test Data: Length of base: 5, Height of parallelogram: 6 # Expected Output: Area is: 30.0 function area_of_parallelogram(base, height) return base * height end # Test the function println("Area is: ", area_of_parallelogram(5, 6)) # OUTPUT: # Area is: 30.0 --------------*************--------------- ASSIGNMENT : 03 # 1. Write a Julia script to create a dictionary. dict = Dict("apple" => 3, "banana" => 5, "cherry" => 2) println("Dictionary: ", dict) # OUTPUT: # Dictionary: Dict("apple" => 3, "banana" => 5, "cherry" => 2) # 2. Write a Julia script to create a dictionary by Arrays. keys = ["name", "age", "city"] values = ["John", 30, "New York"] dict = Dict(keys[i] => values[i] for i in 1:length(keys)) println("Dictionary from arrays: ", dict) # OUTPUT: # Dictionary from arrays: Dict("name" => "John", "age" => 30, "city" => "New York") # 3. Write a Julia script to add a key to a dictionary. dict = Dict("apple" => 3, "banana" => 5) dict["cherry"] = 2 println("Updated Dictionary: ", dict) # OUTPUT: # Updated Dictionary: Dict("apple" => 3, "banana" => 5, "cherry" => 2) # 4. Write a Julia script to concatenate the following dictionaries to create a new one. dict1 = Dict("apple" => 1, "banana" => 2) dict2 = Dict("cherry" => 3, "date" => 4) merged_dict = merge(dict1, dict2) println("Merged Dictionary: ", merged_dict) # OUTPUT: # Merged Dictionary: Dict("apple" => 1, "banana" => 2, "cherry" => 3, "date" => 4) # 5. Write a Julia script to sort (ascending and descending) a dictionary by keys and by value also. dict = Dict("apple" => 3, "banana" => 1, "cherry" => 2) # Sorting by keys (ascending) sorted_by_key_asc = sort(collect(dict), by = x -> x[1]) println("Sorted by keys (ascending): ", sorted_by_key_asc) # Sorting by values (ascending) sorted_by_value_asc = sort(collect(dict), by = x -> x[2]) println("Sorted by values (ascending): ", sorted_by_value_asc) # Sorting by keys (descending) sorted_by_key_desc = sort(collect(dict), by = x -> x[1], rev = true) println("Sorted by keys (descending): ", sorted_by_key_desc) # Sorting by values (descending) sorted_by_value_desc = sort(collect(dict), by = x -> x[2], rev = true) println("Sorted by values (descending): ", sorted_by_value_desc) # OUTPUT: # Sorted by keys (ascending): [("apple", 3), ("banana", 1), ("cherry", 2)] # Sorted by values (ascending): [("banana", 1), ("cherry", 2), ("apple", 3)] # Sorted by keys (descending): [("cherry", 2), ("banana", 1), ("apple", 3)] # Sorted by values (descending): [("apple", 3), ("cherry", 2), ("banana", 1)] # 6. Write a Julia script to check whether a given key already exists in a dictionary. dict = Dict("apple" => 3, "banana" => 1, "cherry" => 2) println("Does 'apple' exist in dictionary? ", haskey(dict, "apple")) println("Does 'orange' exist in dictionary? ", haskey(dict, "orange")) # OUTPUT: # Does 'apple' exist in dictionary? true # Does 'orange' exist in dictionary? false # 7. Write a Julia program to iterate over dictionaries using for loops. dict = Dict("apple" => 3, "banana" => 1, "cherry" => 2) println("Iterating over dictionary:") for (key, value) in dict println("$key => $value") end # OUTPUT: # Iterating over dictionary: # apple => 3 # banana => 1 # cherry => 2 # 8. Write a Julia script to generate and print a dictionary that contains a number (between 1 and n) in the form (x, x*x). function generate_square_dict(n) return Dict(i => i^2 for i in 1:n) end println("Generated Dictionary: ", generate_square_dict(5)) # OUTPUT: # Generated Dictionary: Dict(1 => 1, 2 => 4, 3 => 9, 4 => 16, 5 => 25) # 9. Write a Julia program to create a tuple. tuple_example = (1, "apple", 3.14) println("Tuple: ", tuple_example) # OUTPUT: # Tuple: (1, "apple", 3.14) # 10. Write a Julia program to create a tuple with different data types. tuple_example = (42, "hello", 3.14, true) println("Tuple with different data types: ", tuple_example) # OUTPUT: # Tuple with different data types: (42, "hello", 3.14, true) # 11. Write a Julia program to add an item to a tuple. tuple_example = (1, 2, 3) new_tuple = tuple_example..., 4 # Add item to tuple println("Updated Tuple: ", new_tuple) # OUTPUT: # Updated Tuple: (1, 2, 3, 4) # 12. Write a Julia program to get the 4th element from the last element of a tuple. tuple_example = (10, 20, 30, 40, 50, 60) println("4th element from the last: ", tuple_example[end-3]) # OUTPUT: # 4th element from the last: 40 # 13. Write a Julia program to find the index of an item in a tuple. tuple_example = ("apple", "banana", "cherry", "date") index = findfirst(x -> x == "cherry", tuple_example) println("Index of 'cherry': ", index) # OUTPUT: # Index of 'cherry': 3 # 14. Write a Julia program to convert a list of tuples into a dictionary. tuple_list = [("a", 1), ("b", 2), ("c", 3)] dict_from_tuples = Dict(tuple_list) println("Dictionary from tuples: ", dict_from_tuples) # OUTPUT: # Dictionary from tuples: Dict("a" => 1, "b" => 2, "c" => 3) --------------*************---------------