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

CT Lab for CHEMICAL ENGG

The document lists a series of Python programming experiments covering basic concepts, conditional statements, and looping. It includes detailed example programs for tasks such as calculating the area of shapes, finding GCD, and checking for prime numbers. Each experiment is numbered and includes specific code implementations for various programming challenges.

Uploaded by

janani williams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views20 pages

CT Lab for CHEMICAL ENGG

The document lists a series of Python programming experiments covering basic concepts, conditional statements, and looping. It includes detailed example programs for tasks such as calculating the area of shapes, finding GCD, and checking for prime numbers. Each experiment is numbered and includes specific code implementations for various programming challenges.

Uploaded by

janani williams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

S.

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

Ex.No:1 Basic Python Programs


1.(a). Program to print name, department, roll no, college name
print(“NAME:JANANI S W”)
print(“DEPARTMENT:CHEMICAL”)
print(“ROLL NO:19CH019”)
print(“COLLEGE:KPRIET”)

1.(b). Program for subtraction of two numbers


a=int(input("Enter number 1"))
b=int(input("Enter number 2"))
c=a-b
print("Subtraction of two numbers",c)

1.(c). Program to find area of a square


s=int(input("Enter side"))
area=s*s
print("Area of square is",area)

1.(d). Program to find area of circle


r=int(input(“Enter radius”))
pi=3.14
area=pi*r*r
print(“Area of circle is”,area)

1.(e). Program to swap two variables


x=5
y = 10
temp = x
x=y
y = temp
print('The value of x after swapping',x)
print('The value of y after swapping',y)
Ex.No:2 Programs on Conditional Statements

2.(a). Program to find whether the given number is even or odd


a=int(input("Enter number"))
if(a%2 == 0) :
print("Number is Even")
else:
print("Number is Odd")

2.(b). Program to find the given number is positive or negative


a=int(input("Enter number"))
if(a>=0) :
print("Number is positive")
else:
print("Number is negative")

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

3.(a). Program to print 0 to 10 numbers

i=0
while(i<=10):
print(i)
i=i+1
print("End of while")

3.(b). Program to print sum and average of 0 to 10 numbers


i=0
s=0
while(i<=10):
s=s+i
i=i+1
avg=s/10
print("SUM",s)
print("AVG",avg)

3.(c). Program to print factorial of a number


num = int(input("enter a number: "))
fac = 1
for i in range(1, num + 1):
fac = fac * i
print("factorial of ", num, " is ", fac)
Ex.No:4(a) Program to find sum of digits

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

Number = int(input(" Please Enter any Number: "))


i=1
while(i <= Number):
count = 0
if(Number % i == 0):
j=1
while(j <= i):
if(i % j == 0):
count = count + 1
j=j+1

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

from math import sqrt


print("Input lengths of shorter triangle sides:")
a = float(input("a: "))
b = float(input("b: "))
c = sqrt(a**2 + b**2)
print("The length of the hypotenuse is", c )
Ex.No:15 Print repeating characters in a string

Program

string = "Great responsibility";


print("Duplicate characters in a given string: ");
for i in range(0, len(string)):
count = 1;
for j in range(i+1, len(string)):
if(string[i] == string[j] and string[i] != ' '):
count = count + 1;
#Set string[j] to 0 to avoid printing visited character
string = string[:j] + '0' + string[j+1:];
if(count > 1 and string[i] != '0'):
print(string[i]);

You might also like