0% found this document useful (0 votes)
15 views5 pages

JuliaLab Exp3&4

Uploaded by

Vishal S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

JuliaLab Exp3&4

Uploaded by

Vishal S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Julia Lab Programs

3. a. Develop a Julia program to request a score in a test and print a letter grade based on the
following:
F: score < 50 B:50<=score<75 A: score >= 75

PROGRAM:

println("Enter the score in test")


score = parse(Int, readline())
if score<50
println("Grade obtained is F")
elseif score<75
println("Grade obtained is B")
else
println("Grade obtained is A")
end

OUTPUT:
Enter the score in test
stdin> 67
Grade obtained is B

3. b. Develop a Julia Program for Given three integer values representing the sides of a
triangle, print:
• Not a triangle: if the values cannot be the sides of any triangle. This is so if any value is
negative or zero, or if the length of any side is greater than or equal to the sum of the other
two.
• Scalene: if the triangle is scalene (all sides different)
• Right angled: if the triangle is right-angled
• Isosceles: if the triangle is isosceles (two sides equal)
• Equilateral: if the triangle is equilateral (three sides equal).

PROGRAM:

# Request 3 sides; determine type of triangle


function classifyTriangle()
print("Enter 3 sides of a triangle: ")
a, b, c = [parse(Int, x) for x in split(readline())]
if a <= 0 || b <= 0 || c <= 0
print("\nNot a triangle\n")
elseif a >= b + c || b >= c + a || c >= a + b
print("\nNot a triangle\n")
elseif a^2==b^2+c^2 || b^2==c^2+a^2 || c^2==a^2+b^2
print("\nRight-angled\n")
elseif a == b && b == c

print("\nEquilateral\n")
elseif a == b || b == c || c == a
print("\nIsosceles\n")
else
print("\nScalene\n")
end
end #classifyTriangle
classifyTriangle()

OUTPUT:

Enter 3 sides of a triangle:


stdin> 7 4 7

Isosceles

4. a. An amount of money P (for principal) is put into an account which earns interest at r%
per annum. So, at the end of one year, the amount becomes P + P×r/100. This becomes the
principal for the next year. Develop a Julia program to print the amount at the end of each
year for the next 10 years. However, if the amount ever exceeds 2P, stop any further printing.
Your program should prompt for the values of P and r.

PROGRAM:

function calculatePay()
print("Hours worked? ")
hours = parse(Float64, readline())
print("Rate of pay? ")
rate = parse(Float64, readline())
if hours <= 40
regPay = hours * rate
ovtPay = 0
else
regPay = 40 * rate
ovtPay = (hours - 40) * rate * 1.5
end
grossPay = regPay + ovtPay
println("\nRegular pay: ", regPay)
println("Overtime pay: ", ovtPay)
println("Gross pay: ", grossPay)
end # calculatePay
calculatePay()
OUTPUT:

Hours worked?
stdin> 45
Rate of pay?
stdin> 5.5

Regular pay: 220.0


Overtime pay: 41.25
Gross pay: 261.25

4. b. Develop a Julia program which reads numbers from a le (input.txt) and nds the largest
number, smallest number, count, sum and average of numbers.

PROGRAM:
# To access and display le elements
function inputlist()
println("List of input numbers")
for num in eachline("input.txt")
num = parse(Int, num)
println(num)
end
end

# To calculate smallest element


function minimum()
max = typemax(Int64)
for num in eachline("input.txt")
num = parse(Int, num)
if num < max
max = num
end
end
println("\nMinimum element is $max")
end

# To calculate largest element


function maximum()
min = typemin(Int64)
for num in eachline("input.txt")
num1 = parse(Int,num)
if num1 > min
min = num1
end
end
println("Maximum element is $min")
end
fi
fi
fi
#To calculate total number of elements
function count()
cnt = 0
for num in eachline("input.txt")
cnt+= 1
end
println("Total number of elements in le= $cnt")
end

#To calculate sum of elements


function sum()
sum = 0
for num in eachline(“input.txt")
num1 = parse(Int,num)
sum = sum + num1
end
println("Sum of all numbers= $sum")
end

#To calculate average of elements


function average()
sum = 0
cnt = 0
for num in eachline("input.txt")
num1 = parse(Int,num)
sum = sum + num1
cnt+= 1
end
avg = sum/cnt
println("Average of all numbers= $avg")
end

# Regular ow of program
# Function calls
inputlist()
minimum()
maximum()
count()
sum()
average()

OUTPUT:

List of input numbers


12
32
33
13
56
89
fl
fi
32
44
10
56

Minimum element is 10
Maximum element is 89
Total number of elements in file= 10
Sum of all numbers= 377
Average of all numbers= 37.7

You might also like