2. INDEX
1 Program to accept the number from user and check it’s a palindrome or not
2 Program to accept the number from user and check it’s an ARMSTRONG or not.
3 Program to accept the decimal number from user and display its binary number
4 Program to accept the binary number from user and display its decimal number
5 program to print series 0,3,8,15,24,35,48,63,80,99....N
6
program to print output as given
1
12
123
1234
12345
7
program to print output as given
1
121
12321
1234321
123454321
8 Program to print output as given (1)+(1+2)+(1+2+3)+(1+2+3+4)+(1+2+3+4+5)=55
9 Program to create Simple Calculator(+,-,*,/)
10 Program to Check the year is a Leap Year or not
11 Program to Compute Sum of Series1+ x2
/3! + x3
/5! + x4
/3! + x5
/4! + .. + xn
/2n-1!
3. --[[Program to accept the number from user and check it’s a palindrome or not.]]--
no=0
tmp=0
oldval=0
print("Enter the number")
no=tonumber(io.read())
oldval=no
while(no ~= 0)
do
x=math.floor(no%10)
tmp=tmp*10+x
no=math.floor(no/10)
end
if(tmp == oldval)
then
io.write("It’s a palindrome")
else
io.write("It’s a not palindrome")
end
-------------------output------------------
Enter the number
1221
It’s a palindrome
4. --[[Program to accept the number from user and check its a ARMSTRONG or not.]]--
no=0
tmp=0
oldval=0
print("Enter the number")
no=tonumber(io.read())
oldval=no
while(no ~= 0)
do
x=math.floor(no%10)
tmp=tmp+x^3
no=math.floor(no/10)
end
if(tmp == oldval)
then
io.write("Its a armstrong")
else
io.write("Its a not a armstrong")
end
-------------------output------------------
Enter the number
153
It’s an Armstrong
5. --[[Program to accept the decimal number from user and display its binary number]]--
no=0
tmp=0
oldval=0
print("Enter the decimal number")
no=tonumber(io.read())
oldval=no
while(no ~= 0)
do
x=math.floor(no%2)
tmp=tmp*10+x
no=math.floor(no/2)
end
no=tmp
tmp=0
while(no ~= 0)
do
x=math.floor(no%10)
tmp=tmp*10+x
no=math.floor(no/10)
end
print("binary of "..oldval.."= "..tmp)
--------------------output--------------------------
Enter the decimal number
23
binary of 23= 10111
6. --[[Program to accept the binary number from user and display its decimal number]]--
no=0
tmp=0
oldval=0
a=0
print("Enter the binary number")
no=tonumber(io.read())
oldval=no
while(no ~= 0)
do
x=math.floor(no%10)
tmp=tmp+(x*2^a)
no=math.floor(no/10)
a=a+1
end
print("Decimal of "..oldval.."= "..tmp)
------------------------output------------------
Enter the binary number
10111
Decimal of 10111= 23
7. --[[program to print series 0,3,8,15,24,35,48,63,80,99....N]]--
N=0
tmp=0
a=0
a1=0
x=0
print("Enter the value of N")
N=tonumber(io.read())
while(x<=N)
do
tmp=x+a
io.write(tmp..",")
x=x+1
a1=a1+2
a=a+a1
end
8. --[[program to print Pentagonal number Series 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176]]—
N=0
tmp=0
print("Enter the value of N")
N=tonumber(io.read())
x=0
while(x<=N)
do
tmp=(3*x*x-x)/2
io.write(tmp..",")
x=x+1
end
------------------output--------------------
Enter the value of N
10
0,1,5,12,22,35,51,70,92,117,145
9. --[[program to print output as given
1
12
123
1234
12345
]]--
x=1
k=4
while(x<=5)
do
a=1
while(a<=k)
do
io.write(" ")
a=a+1
end
k=k-1
y=1
while(y<=x)
do
io.write(y)
y=y+1
end
print()
x=x+1
end
10. --[[program to print output as given
1
121
12321
1234321
123454321
]]--
x=1
k=4
while(x<=5)
do
a=1
while(a<=k)
do
io.write(" ")
a=a+1
end
k=k-1
y=1
while(y<=x)
do
io.write(y)
y=y+1
end
z=x-1
while(z>=1)
do
io.write(z)
z=z-1
end
print()
x=x+1
end
11. --[[program to print output as given (1)+(1+2)+(1+2+3)+(1+2+3+4)+(1+2+3+4+5)=55]]--
x=1
insum=0
outsum=0
while(x<=5)
do
y=1
insum=0
io.write("(")
while(y<=x)
do
insum=insum+x
io.write(y.."+")
y=y+1
end
io.write("b)+")
outsum=outsum+insum
x=x+1
end
print("b="..outsum)
12. --[[ Program to create Simple Calculator(+,-,*,/) ]]--
ch=' '
op1=0
op2=0
print("Enter the op1 and op2")
op1=tonumber(io.read())
op2=tonumber(io.read())
print("Enter the operator(+,-,/,*)")
ch=io.read()
if(ch=="+")
then
print(op1.."+"..op2.."="..(op1+op2))
elseif(ch=="-")
then
print(op1.."-"..op2.."="..(op1-op2))
elseif(ch=="/")
then
print(op1.."/"..op2.."="..(op1/op2))
elseif(ch=="*")
then
print(op1.."*"..op2.."="..(op1*op2))
else
print("invalid operator")
end
----------------------output----------------------
Enter the op1 and op2
10
20
Enter the operator(+,-,/,*)
+
10+20=30
13. --[[Program to Check Leap Year]]--
yy=0
print("Enter the year")
yy=tonumber(io.read())
if(yy%100 == 0 or yy%4==0 or yy%400==0)
then
print("its a leap year")
else
print("its not a leap year")
end
---------------------output----------------------
Enter the year
1988
its a leap year
14. --[[Program to Compute Sum of Series 1 + x2/3! + x3/5! + x4/3! + x5/4! + .. + xn/2n-1!]]--
sum=1.00
a=2
fact=1
y=0
print("Enter the value of x,N")
x=tonumber(io.read())
N=tonumber(io.read())
while(a<=N)
do
tmp=x^a
fact=1
y=2*a-1
while(y>=1)
do
fact=fact*y
y=y-1
end
sum = sum +tmp/fact
a=a+1
end
print("sum="..sum)
-------------------output-------------------------
Enter the value of x,N 2 10
sum=1.7365977440172