Python LabExp2
Python LabExp2
: 7
GENERATING FIBONACCI SERIES
DATE:
AIM:
To write a Python program for generating first ‘N’ Fibonacci numbers.
ALGORITHM:
Step 1 :
Start the program
Step 2 :
Read the number of terms, n
Step 3 :
Assign f0=0 and f1=1
Step 4 :
Print the values of f0 and f1
Step 5 :
for i in range (2,n,1) do
compute f=f0+f1
assign f0=f1, f1=f
print the value of f
Step 6 : Stop the program
PYTHON CODE:
OUTPUT:
RESULT:
Thus the Python program for generating first ‘N’ Fibonacci numbers is written and output is
verified.
EX.NO.: 8
PERFECT NUMBER OR NOT
DATE:
AIM:
To write a Python program to check whether the given number is Perfect or not.
ALGORITHM:
Step 1 :
Start the program
Step 2 :
Read a number, n
Step 3 :
Initialize sum=0
Step 4 :
for i in range (1,n,1) goto step 5, else goto step 6
Step 5 :
if(n%i==0)
compute sum=sum+i
set i=i+1, goto step 4
else
set i=i+1, goto step 4
Step 6 : if (sum==n)
Print n is a perfect number
else
Print n is not a perfect number
Step 7 : Stop the program
PYTHON CODE:
n=int(input("Enter a number:"))
sum=0
for i in range(1,n,1):
if(n%i==0):
sum=sum+i
if(sum==n):
print(n,"is a perfect number")
else:
print(n,"is not a perfect number")
OUTPUT:
Enter a number: 8
8 is not a perfect number
Enter a number: 6
6 is a perfect number
RESULT:
Thus the Python program to check whether the given number is Perfect or not is written and
output is verified.
EX.NO.: 10
STRONG NUMBER OR NOT
DATE:
AIM:
To write a Python program to check whether the given number is Strong or not.
ALGORITHM:
import math
n=int(input("Enter a number:"))
n1=n
sum=0
while(n>0):
rem=n%10
fact=math.factorial(rem)
sum=sum+fact
n=n//10
if(sum==n1):
print(n1,"is a strong number")
else:
print(n1,"is not a strong number")
OUTPUT:
RESULT:
Thus the Python program to check whether the given number is strong or not is written and
output is verified.
EX.NO.: 11
PRIME NUMBER OR NOT
DATE:
AIM:
To write a Python program to check whether the given number is Prime or not.
ALGORITHM:
Step 1 :
Start the program
Step 2 :
Read a number, n
Step 3 :
Initialize flag=0
Step 4 :
for i in range (2,n,1), goto step 5, else goto step 6
Step 5 :
if(n%i==0)
set flag=1, goto step 6
else
set i=i+1, goto step 4
Step 6 : if(flag==0)
Print n is a prime number
else
Print n is not a prime number
Step 7 : Stop the program
PYTHON CODE:
n=int(input("Enter a number:"))
flag=0
for i in range(2,n,1):
if(n%i==0):
flag=1
break
if(flag==0):
print(n,"is a prime number")
else:
print(n,"is not a prime number")
OUTPUT:
Enter a number: 5
5 is a prime number
Enter a number: 6
6 is not a prime number
RESULT:
Thus the Python program to check whether the given number is prime or not is written and
output is verified.
EX.NO.: 12
FINDING MINIMUM AND MAXIMUM IN A LIST
DATE:
AIM:
To write a Python program for finding minimum and maximum in a list.
ALGORITHM:
L=[]
n=int(input("Enter the list size:"))
for i in range(0,n,1):
element=int(input("Enter list item:"))
L.append(element)
print("The List is",L)
L.sort()
print("The minimum element in the list is",L[0])
print("The maximum element in the list is",L[n-1])
OUTPUT:
RESULT:
Thus the Python program for finding minimum and maximum in a list is written and output is
verified.
EX.NO.: 13
TRAFFIC LIGHT CONTROL SYSTEM (CHAINED IF-ELIF-ELSE)
DATE:
AIM:
To write a Python program for implementing Traffic Light Control System.
ALGORITHM:
c=input("Red/Green/Yellow:")
if(c=="Red"):
print("Stop")
elif(c=="Yellow"):
print("Ready")
elif(c=="Green"):
print("Go")
else:
print("Red/Green/Yellow only accepted")
OUTPUT:
Red/Green/Yellow: Red
Stop
Red/Green/Yellow: Yellow
Ready
Red/Green/Yellow: Green
Go
Red/Green/Yellow: Orange
Red/Green/Yellow only accepted
RESULT:
Thus the Python program for implementing Traffic Light Control System is written and output
is verified.
EX.NO.:5A
EXCHANGING VALUES OF TWO VARIABLES USING TEMPORARY VARIABLE
DATE:
AIM:
To write a Python program for exchanging values of two variables using temporary variable.
ALGORITHM:
Step 1 :
Start the program
Step 2 :
Read the values of a,b
Step 3 :
Print the values of a,b
Step 4 :
Assign temp=a
a=b
b=temp
Step 5 : Print the values of a,b
Step 6 : Stop the program
PYTHON CODE:
a = int(input("Enter the value of a: "))
b = int(input("Enter the value of b: "))
print("Values of a and b before swapping:",a,b)
temp = a
a=b
b = temp
print("Values of a and b after swapping:",a,b)
OUTPUT:
Enter the value of a: 2
Enter the value of b: 6
Values of a and b before swapping: 2 6
Values of a and b after swapping: 6 2
RESULT:
Thus the Python program for exchanging values of two variables using temporary variable is
written and output is verified.
EXCHANGING VALUES OF TWO VARIABLES WITHOUT USING TEMPORARY
VARIABLE
DATE:
AIM:
To write a Python program for exchanging values of two variables without using temporary
variable.
ALGORITHM:
Step 1 : Start the program
Step 2 : Read the values of a,b
Step 3 : Print the values of a,b
Step 4 : Compute a=a+b
b=a-b
a=a-b
Step 5 : Print the values of a,b
Step 6 : Stop the program
PYTHON CODE:
a = int(input("Enter the value of a: "))
b = int(input("Enter the value of b: "))
print("Values of a and b before swapping:",a,b)
a=a+b
b=a-b
a=a-b
print("Values of a and b after swapping:",a,b)
OUTPUT:
Enter the value of a: 3
Enter the value of b: 5
Values of a and b before swapping: 3 5
Values of a and b after swapping: 5 3
RESULT:
Thus the Python program for exchanging values of two variables without using temporary
variable is written and output is verified.