0% found this document useful (0 votes)
14 views

Python Lab Programs

Uploaded by

ANITHA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python Lab Programs

Uploaded by

ANITHA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SAMPLE PROGRAMS

SWAP TWO NUMBERS USING A TEMPORARY VARIABLE

AIM
To sway two variables using a temporary variable

PROGRAM
num1=int(input(“Enter num1”))
num2=int(input(“Enter num2”))
print(“Before Swapping”)
print(“num1=”,num1)
print(“num2=”,num2)
temp=num1
num1=num2
num2=temp
print(“After Swapping”)
print(“num1=”,num1)
print(“num2=”,num2)

RESULT

LEAP YEAR OR NOT

AIM
To find whether a given year is leap year or not

PROGRAM
year=int(input("Enter year"))
if(year%4==0):
if(year%100==0):
if(year%400==0):
print("The given year is a leap year")
else:
print("The given year is not a leap year ")
else:
print("The given year is a leap year ")
else:
print("The given year is not a leap year ")

RESULT
LAB PROGRAMS

EX. NO: 1 GCD OF TWO NUMBERS

PROGRAM
no1=int(input('Enter first no:'))
no2=int(input('Enter second no:'))
def computeGCD(x,y):
if x>y:
smaller=y
else:
smaller=x
for i in range(1,smaller+1):
if((x%i==0)and(y%i==0)):
gcd=i
return gcd
print"The gcd of",no1,"and",no2,"is",computeGCD(no1,no2)

OUTPUT
[root@cselab14 Python-3.6.1]# python gcdofnumbers.py
Enter first no: 8
Enter second no: 12
The gcd of 8 and 12 is 4

RESULT

EX. NO: 2 SQUARE ROOT OF A NUMBER (NEWTON’S METHOD)

PROGRAM
num=float(input('Enter a no:\t'))
approx=0.5*num
better=0.5*(approx+num/approx)
while (better!=approx):
approx=better
better=0.5*(approx+num/approx)
print (approx)

OUTPUT
[root@cselab14 Python-3.6.1]# python squareroot.py
Enter a no: 3
1.73205080757

RESULT
EX. NO: 3 EXPONENTIATION (POWER OF A NUMBER)

PROGRAM
base=int(input("Enter base:"))
exp=int(input("Enter exponent:"))
ans=1
for i in range(1,exp+1):
ans=ans*base
print "Result of exponentiation:", ans

OUTPUT
[root@cselab1-46 Python-3.6.1]#python exponentiation.py
Enter base:2
Enter exponent:5
Result of exponentiation:32

RESULT

You might also like