CT Lab for CHEMICAL ENGG
CT Lab for CHEMICAL ENGG
No List of Experiments
1. Basic python programs
2. Programs on conditional statements
3. Programs on looping
a. Program to find sum of digits
b. Program to reverse the number
4. c. Program to find palindrome or not
d. Program to find Armstrong number or not
e. Program to print prime numbers
5. Program to print GCD of numbers
6. Program to print square root of numbers
7. Program to print exponent of a number
8. Program to find maximum number in a range
9. Program to print index of search element
10. Program to print differences of indices of largest and smallest number in a array
11. Program to print prime factors of given number
12. Product and sum of diagonal matrix
13. Addition of two matrices
14. Pythagoras theorem to calculate hypotenuse of a right angle triangle
15. Print repeating characters in a string
2.(c). Program to check the whether the given number is positive, zero or negative.
num = 3.4
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Ex.No:3 Programs on looping statements
i=0
while(i<=10):
print(i)
i=i+1
print("End of while")
Program:
n=int(input("Enter a number:"))
tot=0
while(n>0):
dig=n%10
tot=tot+dig
n=n//10
print("The total sum of digits is:",tot)
Ex.No:4(b) Program to reverse the number
Program:
n=int(input("Enter number: "))
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
print("Reverse of the number:",rev)
Ex.No:4(c) Program to find palindrome or not
Program
n=int(input("Enter Number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is palindrome!")
else:
print("The number is not a palindrome!")
Ex.No:4(d) Program to find Armstrong number or not
Program
n=int(input("Enter number"))
s=0
num=n
while(n>0):
r=n%10
s=s+(r**3)
n=n/10
if(s==num):
print("The number is Armstrong")
else:
print("The number is not Armstrong")
Ex.No:4(e) Program to prime numbers
n=int(input())
for num in range(1,n+1):
for i in range(2,num):
if num%i==0:
break
else:
print num
Ex.No:5 Program to print GCD of numbers
Program
def gcd(x,y):
if x>y:
s=y
else:
s=x
for i in range(1,s+1):
if(x%i==0 and y%i==0):
gcd=i
return gcd
n1=input("Enter the first number")
n2=input("Enter the second number")
print(gcd(n1,n2))
Ex.No:6 Program to print square root of numbers
Program
def sqrtn(a,x):
while True:
print(x)
y=float(x+a/x)/2
if y==x:
break
x=y
a=int(input())
x=int(input())
sqrtn(a,x)
Ex.No:7 Program to print exponent of a number
Program
def powerexp(base,exp):
if(exp==1):
return(base)
if(exp!=1):
return(base*powerexp(base,exp-1))
base=int(input("Enter base Value: "))
exp=int(input("Enter exponential Value: "))
print "Result:",powerexp(base,exp)
Ex.No:8 Program to find maximum number in a range
Program
list=[]
print "Enter the Upper limit"
n=int(input(''))
print"Enter the numbers"
for i in range(0,n):
a=int(input())
list.append(a)
max=list[0]
for i in range(len(list)):
if(list[i]>max):
max=list[i]
print "Maximum number in a list",max
Ex.No:9 Program to print index of search element
Program
def lsearch(list,x):
found=False
for i in range(len(list)):
if(list[i] == x):
found = True
break
if(found==True):
print("%d found at %dth position"%(x,i))
else:
print("%d is not in list"%x)
list=[]
print "Enter the number of elements in list"
n=int(input())
print "Enter the Elements"
for i in range(n):
a=int(input())
list.append(a)
x=int(input('Enter the element to be searched'))
lsearch(list,x)
Ex.No:10 Program to print differences of indices of largest and smallest number in a
array
Program
n = int(input())
li = list(map(int,input().split()))
small = min(li)
large = max(li)
print(((li.index(large)+1) - (li.index(small)+1), end = " ")
Ex.No:11 Program to print prime factors of given number
Program
if (count == 2):
print(" %d is a Prime Factor of a Given Number %d" %(i, Number))
i=i+1
Ex.No:12 Product and sum of diagonal matrix
Program
MAX = 100
def printDiagonalSums(mat, n):
principal = 0
secondary = 0;
for i in range(0, n):
for j in range(0, n):
# Condition for principal diagonal
if (i == j):
principal += mat[i][j]
# Condition for secondary diagonal
if ((i + j) == (n - 1)):
secondary += mat[i][j]
print("Principal Diagonal:", principal)
print("Secondary Diagonal:", secondary)
# Driver code
a = [[ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ]]
printDiagonalSums(a, 4)
Ex.No:13 Addition of two matrices
Program
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
Ex.No:14 Pythagoras theorem to calculate hypotenuse of a right angle triangle
Program
Program