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

Python LabExp2

Python

Uploaded by

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

Python LabExp2

Python

Uploaded by

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

EX.NO.

: 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:

n=int(input("Enter the number of terms:"))


f0=0
f1=1
print("The first",n,"Fibonacci numbers are")
print(f0)
print(f1)
for i in range(2,n,1):
f=f0+f1
f0=f1
f1=f
print(f)

OUTPUT:

Enter the number of terms: 6


The first 6 Fibonacci numbers are
0
1
1
2
3
5

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:

Step 1 : Start the program


Step 2 : Import the module, math
Step 3 : Read a number, n
Step 4 : Assign n1=n
Step 5 : Initialize sum=0
Step 6 : if(n>0), goto step 7, else goto step 8
Step 7 : Compute rem=n%10
fact=math.factorial(rem)
sum=sum+fact
n=n//10, goto step 6
Step 8 : if (sum==n1)
Print n1 is a strong number
else
Print n1 is not a strong number
Step 9 : Stop the program
PYTHON CODE:

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:

Enter a number: 145


145 is a strong number

Enter a number: 154


154 is not a strong number

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:

Step 1 : Start the program


Step 2 : Initialize a list, L=[ ]
Step 3 : Read the list size, n
Step 4 : for i in range (0,n,1) do
Read a list item, element
Append element to the list L, L.append(element)
Step 5 : Print the list, L
Step 6 : Sort the list, L.sort()
Step 7 : Print the values of L[0], L[n-1]
Step 8 : Stop the program
PYTHON CODE:

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:

Enter the list size:7


Enter list item:3
Enter list item:8
Enter list item:9
Enter list item:5
Enter list item:7
Enter list item:1
Enter list item:4
The List is [3, 8, 9, 5, 7, 1, 4]
The minimum element in the list is 1
The maximum element in the list is 9

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:

Step 1 : Start the program


Step 2 : Read a colour, c
Step 3 : if (c=="Red")
Print Stop
elif (c=="Yellow")
Print Ready
elif (c=="Green")
Print Go
else
Print Red/Green/Yellow only accepted
Step 4 : Stop the program
PYTHON CODE:

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.

You might also like