0% found this document useful (0 votes)
3 views20 pages

Final Lua Record1

This document is a lab manual for a Lua programming course at SRM Institute of Science and Technology for BCA students in their fifth semester. It includes various programming exercises covering topics such as data types, control statements, string manipulation, and class usage, along with example programs and their outputs. The manual is structured with an index and aims to provide hands-on experience in Lua programming.
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)
3 views20 pages

Final Lua Record1

This document is a lab manual for a Lua programming course at SRM Institute of Science and Technology for BCA students in their fifth semester. It includes various programming exercises covering topics such as data types, control statements, string manipulation, and class usage, along with example programs and their outputs. The manual is structured with an index and aims to provide hands-on experience in Lua programming.
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/ 20

..

SRM Institute of Science and Technology


Ramapuram Chennai
Faculty of Science & Humanities
(A Place for Transformation)
Department of Computer Science and Applications (BCA)

LAB MANUAL

UCA20S03L- LUA PROGRAMMING


For U.G. Degree Programme Batch (2022– 2025)
BCA (V Semester)
Academic year 2024-25
ODD Semester Regulations-2020
Year/Sem: III Year / V Sem

JUNE 2024

Prepared By Approved By
Dr. V. PAVITHRA (AP/BCA)
Dr.E. SRIMATHI (AP/BCA)

1
INDEX

S.No Date Name of the Program Page No

1 Swapping of Two Number 4

2 Basic Data Types 6

3 Developing Simple Program 8

4 Write a string to perform various String Manipulation 10

5 Arithmetic Operations 12

6 Control Statements 15

7 Write a Program to Reverse a Number 17

8 Write a Program to generate Multiplication table 19

9 Write a Program to work with Tables 21

10 Write a Program to add two Matrix 23

11 Write a program to illustrate the concept Iterations 25

12 Write a Program using Class and Objects 26

Additional Content Beyond the Syllabus

13 Write a Program using Class and Objects Factorial 27


Program

14 Write a Program using Class and Objects Even or odd 29

15 Write a Program using Class and Objects Prime 31


Number or Not Program

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:

Enter the first number :23

Enter the second number :34


Enter the third number :54
54 is Largest among three numbers.

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:

None of the values is matching

Exact value of a is: 100

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:

io.write('Enter the first number :')


num1 = io.read("*n")

str = tostring(num1)

num = string.reverse(str)

print('Reversed number is ',num)

Output:

Enter the first number :345456

Reversed number is 654543

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:

Thus the program has been successfully completed and executed.

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

Write a program to illustrate the concept Iterators


14
Aim:
To write a Lua program to illustrate the concept Iterators.

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

Write a program using class and objects


16
Aim:
To write a Lua program to using class and objects.

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:

The area is 100

Result:
Thus the program has been successfully completed and executed.

EX.NO: 13

Factorial of given Numbers using Class &Objects


17
Aim:
To write a Lua program to display a factorial of given numbers

Program:

defines a factorial function


function fact (n)
if n == 0 then
return 1
else
return n * fact(n-1)
end
end

print("enter a number:")
a = io.read("*number") -- read a number
print(fact(a)

Sample Input

Enter the number :5


Factorial of 5 is :120

EX.NO: 14

Write a program to display odd or Even Numbers Using Class and Objects
18
Aim:

To write a Lua program to display a odd or Even Numbers

Program:

a = 10
if(a%2 == 0)
then
print 'This is an even number'

else

print 'This is an odd number.'

end

Sample Input

Enter the odd Number :5

Sample Output

The Number is odd

EX.NO: 15

19
Write a program to display a Prime Number

Aim:

To write a Lua program to display a Prime Number

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

Enter the prime Number:5

Sample Output

The Number is Prime

20

You might also like