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

Julia Lab-Exp1&2

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)
33 views5 pages

Julia Lab-Exp1&2

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

Experiment-1:

a. Develop a Julia program to simulate a calculator (for integers and real


numbers)
Program: Using Julia REPL command-line

julia> 2+3
5
julia> 2 .+ 3
5
julia> 2. + 3
5.0
julia> 2 + (2+3im)
4 + 3im
julia> 2. + (2+3im) #real number is a oating point number
4 + 3im
julia> 2. + (2.0+3im) #complex number's real part
# is oating point number
4.0 + 3.0im
julia> 2 + (2.0+3im)
4.0 + 3.0im
julia> Rational(2,3)
2//3
julia> Rational(2,3)+2
8//3
julia> Rational(2,3)+2.0
2.6666666666666665
julia> Rational(2,3)+ (2+3im)
8//3 + 3//1*im
julia> Rational(2,3)+ (2+3im) + 3.0
5.666666666666666 + 3.0im
julia> pi
π = 3.1415926535897...
julia> exp(1)
e = 2.718281828459045
julia> e+pi
5.859874482048838
julia> e+2
4.718281828459045
julia> e+2.0
4.718281828459045
julia> e+Rational(2,3)
3.3849484951257116
julia> e+(2+3im)
4.718281828459045 + 3.0im
fl
fl
b. Develop a Julia program to add, subtract, multiply, and divide complex
numbers.
Program:
println("Enter real part of complex number")
a = parse(Int, readline())
println("Enter img part of complex number")
b = parse(Int, readline())
c1 = complex(a,b)
println("First complex number is $c1")
println("Enter real part of second number")
d = parse(Int, readline())
println("Enter img part of second number")
e = parse(Int, readline())
println("Second complex number is $c2")
c2 = complex(d,e)
r1 = c1+c2
println("Addition result is $r1")
r2 = c1-c2
println("Subtraction result is $r2")
r3 = c1*c2
println("Multiplication result is $r3")
r4 = c1/c2
print("Multiplication result is $r4”)

Output:
Enter real part of complex number
stdin> 1
Enter img part of complex number
stdin> 2
First complex number is 1 + 2im
Enter real part of second number
stdin> 3
Enter img part of second number
stdin> 4
Second complex number is 3 + 4im
Addition result is 4 + 6im
Subtraction result is -2 - 2im
Multiplication result is -5 + 10im
Multiplication result is 0.44 + 0.08im
c. Develop a Julia program to evaluate expressions having mixed data types
(integer, real, oating-point number, and complex).

Program:
println("Enter an integer number")
num1 = parse(Int, readline())
println("Enter a decimal number")
num2 = parse(Float64, readline())
println("Enter real part of complex number");
a = parse(Int,readline())
println("Enter imag part of complex number")
b = parse(Int, readline())
num3 = complex(a,b)
# Expression evaluating an integer and a oat number
exp1 = num1 * num2
# Expressing evaluating an integer and a complex number
exp2 = num1 - num3
#expression evaluating a oat and a complex number
exp3 = num2 + num3
#Expressing evaluating an integer, a oat and a complex number
exp4 = num1 * num2 / num3
println("\nExpression evaluation result:")
println(“\nExpression-1 = $exp1")
println(“Expression-2 = $exp2")
println(“Expression-3 = $exp3")
println(“Expression-4 = $exp4”)

Output:

Enter an integer number


stdin> 2
Enter a decimal number
stdin> 3.5
Enter real part of complex number
stdin> 1
Enter imag part of complex number
stdin> 2

Expression evaluation result:

Expression-1 = 7.0
Expression-2 = 1 - 2im
Expression-3 = 4.5 + 2.0im
Expression-4 = 1.4000000000000001 - 2.8000000000000003im
fl
fl
fl
fl
Experiment-2:

a. Develop a Julia program for the following problem: The following data
are given for a customer in a bank: name, account number, average
balance, and number of transactions made during the month. It is required
to calculate the interest earned and service charge. The interest is
calculated as follows: interest = 3% of the average balance and the
service charge is calculated by this: service charge = 75 cents per
transaction Write a program to read the data for the customer, calculate
the interest and service charge, and print the customer’s name, average
balance, interest and service charge.

Program:
println(“Enter the Name of customer ")
customer = readline()
println("Account number? ")
acctNum = readline()
println("Average balance? ")
avgBal = parse(Float64, readline())
println("Number of transactions? ")
numTrans = parse(Int, readline())
interest = avgBalance * 0.03
service = numTrans * 0.75
println("\nName: $customer")
println("Average balance: $avgBal")
println("Interest: $interest")
println("Service charge: $service”)

Output:
Enter the Name of customer
stdin> aman
Account number?
stdin> 12345
Average balance?
stdin> 5400
Number of transactions?
stdin> 15

Name: aman
Average balance: 5400.0
Interest: 162.0
Service charge: 11.25
b. Develop a Julia program to calculate a person’s regular pay, overtime
pay, and gross pay based on the following: If hours worked are less than or
equal to 40, regular pay is calculated by multiplying hours worked by rate of
pay, and overtime pay is 0. If hours worked are greater than 40, regular pay is
calculated by multiplying 40 by the rate of pay, and overtime pay is calculated
by multiplying the hours above 40 by the rate of pay by 1.5. Gross pay is
calculated by adding regular pay and overtime pay.

Solution:
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”)

Output:
Hours worked?
stdin> 52
Rate of pay?
stdin> 300

Regular pay: 12000.0


Overtime pay: 5400.0
Gross pay: 17400.0

You might also like