Final Lua Record1
Final Lua Record1
LAB MANUAL
JUNE 2024
Prepared By Approved By
Dr. V. PAVITHRA (AP/BCA)
Dr.E. SRIMATHI (AP/BCA)
1
INDEX
5 Arithmetic Operations 12
6 Control Statements 15
EX.NO: 01
2
Swapping of Two Number
Aim:
To write a Lua program for swapping of two number.
Program:
local a, b
-- Initialization
a = 10
b = 30
print("value of a:", a)
print("value of b:", b)
-- Swapping of variables
b, a = a, b
print("value of a:", a)
print("value of b:", b)
Output:
value of a: 10
value of b: 30
value of a: 30
value of b: 10
Result:
Thus the program has been successfully completed and executed.
EX.NO: 02
3
Basic Data Types
Aim:
To write program for basic data types in Lua.
Program:
print(type("What is my type")) --> string
t=10
print(type(5.8*t)) --> number
print(type(true)) -->boolean
print(type(print)) --> function
print(type(type)) --> function
print(type(nil)) --> nil
print(type(type(ABC))) --> string
Output:
string
number
boolean
function
function
nil
string
Result:
Thus the program has been successfully completed and executed.
EX.NO: 03
4
Developing Simple Programs
Aim:
To develop a simple program in Lua.
Program:
io.write('Enter the first number :')
num1 = io.read("*n") -- read a number
io.write('Enter the second number :')
num2 = io.read("*n") -- read a number
io.write('Enter the third number :')
num3 = io.read("*n") -- read a number
if(num1 > num2 and num1 > num3)
then
io.write(num1, ' is Largest among three numbers.')
elseif(num2 > num3)
then
io.write(num2, ' is Largest among three numbers.')
else
io.write(num3, ' is Largest among three numbers.')
end
Output:
Result:
Thus the program has been successfully completed and executed.
EX.NO: 04
5
Write a program to perform various string manipulations
Aim:
To write a Lua program to perform various string manipulations.
Program:
string1 = "Lua";
print(string.upper(string1))
print(string.lower(string1))
string = "Lua Tutorial"
-- replacing strings
newstring = string.gsub(string,"Tutorial","Language")
print("The new string is",newstring)
string = "Lua Tutorial"
-- replacing strings
print(string.find(string,"Tutorial"))
reversedString = string.reverse(string)
print("The new string is",reversedString)
string1 = "Lua"
string2 = "Tutorial"
number1 = 10
number2 = 20
-- Basic string formatting
print(string.format("Basic formatting %s %s",string1,string2))
-- Date formatting
date = 2; month = 1; year = 2014
print(string.format("Date formatting %02d/%02d/%03d", date, month,
year))
-- Decimal formatting
print(string.format("%.4f",1/3))
Output:
LUA
lua
The new string is Lua Language
5 12
The new string is lairotuTauL
Basic formatting Lua Tutorial
Date formatting 02/01/2014
0.3333
Result:
Thus the program has been successfully completed and executed.
EX.NO: 05
6
Write a program to perform simple arithmetic operations
Aim:
To write a Lua program to perform simple arithmetic operations.
Program:
a = 21
b = 10
c=a+b
print("Line 1 - Value of c is ", c )
c=a-b
print("Line 2 - Value of c is ", c )
c=a*b
print("Line 3 - Value of c is ", c )
c=a/b
print("Line 4 - Value of c is ", c )
c=a%b
print("Line 5 - Value of c is ", c )
c = a^2
print("Line 6 - Value of c is ", c )
c = -a
print("Line 7 - Value of c is ", c )
Output:
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2.1
Line 5 - Value of c is 1
Line 6 - Value of c is 16679880978201
Line 7 - Value of c is -21
Result:
Thus the program has been successfully completed and executed.
EX.NO: 06
7
Control Statements
Aim:
To write a Lua program to implement control statements.
a. FOR LOOP
Program:
fori=10,1,-1
do
print(i)
end
Output:
10
9
8
7
6
5
4
3
2
1
b. WHILE LOOP
Program:
a=10
while( a < 20 )
do
print("value of a:", a)
a = a+1
end
Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
8
value of a: 18
value of a: 19
c. REPEAT…UNTIL LOOP
Program:
--[ local variable definition --]
a = 10
--[ repeat loop execution --]
repeat
print("value of a:", a)
a=a+1
until( a > 15 )
Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
d. IF/ELSE
Program:
a = 100
--[ check the boolean condition --]
if( a == 10 )
then
--[ if condition is true then print the following --]
print("Value of a is 10" )
elseif( a == 20 )
then
--[ if else if condition is true --]
print("Value of a is 20" )
elseif( a == 30 )
then
--[ if else if condition is true --]
print("Value of a is 30" )
else
--[ if none of the conditions is true --]
print("None of the values is matching" )
9
end
print("Exact value of a is: ", a )
Output:
Result:
Thus the program has been successfully completed and executed.
EX.NO: 07
10
Write a program to reverse a number
Aim:
To write a Lua program to reverse a number.
Program:
str = tostring(num1)
num = string.reverse(str)
Output:
Result:
Thus the program has been successfully completed and executed.
EX.NO: 08
11
Write a program to generate multiplication table
Aim:
To write a Lua program to generate multiplication table.
Program:
local number=11
print('PRINTING TABLE OF',number)
count=1
while (count~=10)
do
finals=number*count
print(number,"X",count,"=",finals)
count= count+1
end
Output:
PRINTING TABLE OF 11
11 X 1 = 11
11 X 2 = 22
11 X 3 = 33
11 X 4 = 44
11 X 5 = 55
11 X 6 = 66
11 X 7 = 77
11 X 8 = 88
11 X 9 = 99
Result:
Thus the program has been successfully completed and executed.
EX.NO: 09
12
Write a program to work with tables
Aim:
To write a Lua program to work with tables.
Program:
mytable = {}
print("Type of mytable is ",type(mytable))
mytable[1]= "Lua"
mytable["wow"] = "Tutorial"
print("mytable Element at index 1 is ", mytable[1])
print("mytable Element at index wow is ", mytable["wow"])
-- alternatetable and mytable refers to same table
alternatetable = mytable
print("alternatetable Element at index 1 is ", alternatetable[1])
print("mytable Element at index wow is ", alternatetable["wow"])
alternatetable["wow"] = "I changed it"
print("mytable Element at index wow is ", mytable["wow"])
-- only variable released and and not table
alternatetable = nil
print("alternatetable is ", alternatetable)
-- mytable is still accessible
print("mytable Element at index wow is ", mytable["wow"])
mytable = nil
print("mytable is ", mytable)
Output:
Type of mytable is table
mytable Element at index 1 is Lua
mytable Element at index wow is Tutorial
alternatetable Element at index 1 is Lua
mytable Element at index wow is Tutorial
mytable Element at index wow is I changed it
alternatetable is nil
mytable Element at index wow is I changed it
mytable is nil
Result:
EX.NO: 10
13
Write a program to add two matrix
Aim:
To write a Lua program to add two matrix.
Program:
arr1 = {{1,3,4},{2,4,3},{3,4,5}}
arr2 = {{1,3,4},{2,4,3},{1,2,4}}
add = {}
fori=1,3 do
add[i]={}
for j=1,3 do
add[i][j] = arr1[i][j] + arr2[i][j]
end
end
-- Accessing the array
fori=1,3 do
for j=1,3 do
print(add[i][j])
end
end
Output:
2
6
8
4
8
6
4
6
9
Result:
Thus the program has been successfully completed and executed.
EX.NO: 11
a. Stateful Iterator
Program:
array = {"Lua", "Tutorial"}
functionelementIterator (collection)
local index = 0
local count = #collection
-- The closure function is returned
return function ()
index = index + 1
if index <= count
then
-- return the current element of the iterator
return collection[index]
end
end
end
for element in elementIterator(array)
do
print(element)
end
Output:
Lua
Tutorial
b. Stateless iterator
Program:
function square(iteratorMaxCount,currentNumber)
ifcurrentNumber<iteratorMaxCount
then
currentNumber = currentNumber+1
returncurrentNumber, currentNumber*currentNumber
end
15
end
fori,n in square,3,0
do
print(i,n)
end
Output:
1 1
2 4
3 9
Result:
Thus the program has been successfully completed and executed.
EX.NO: 12
Program:
-- Meta class
Shape = {area = 0}
-- Base class method new
functionShape:new (o,side)
o = o or {}
setmetatable(o, self)
self.__index = self
self.area = side*side;
return o
end
-- Base class method printArea
functionShape:printArea ()
print("The area is ",self.area)
end
-- Creating an object
myshape = Shape:new(nil,10)
myshape:printArea()
Output:
Result:
Thus the program has been successfully completed and executed.
EX.NO: 13
Program:
print("enter a number:")
a = io.read("*number") -- read a number
print(fact(a)
Sample Input
EX.NO: 14
Write a program to display odd or Even Numbers Using Class and Objects
18
Aim:
Program:
a = 10
if(a%2 == 0)
then
print 'This is an even number'
else
end
Sample Input
Sample Output
EX.NO: 15
19
Write a program to display a Prime Number
Aim:
Program:
num=tonumber(io.read("*n"))
boo=true
limit=num-1
start=2
for i = start,limit,1
do
if( num%i == 0) then
boo= false
end
end
if (boo==true)
then
print("IT IS A PRIME DA NUMBER")
else
print("NOT A PRIME NO.")
end
Sample Input
Sample Output
20