Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

FIBONACCI SERIES

7
Aim:
To write a program to print the Fibonacci series upto ‘n’ terms and also print the sum of
Fibonacci series.
Algorithm:
STEP 1: Open the python IDLE.
STEP 2: Get the upper-limit.
STEP 3: Assign res, x as 0 and y as 1.
STEP 4: Using the loop construct from 1 to upper limit-1 add x and y to generate the new
number, print the new number and add with the res to find the sum of the series.
STEP 5: Print the sum of the series.
STEP 6: Stop
Expected input:
Enter the upper limit - 20
Expected output:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
10944
Code:
# PROGRAM 7
up_limit = int(input("Enter the count of Fibonacci no : "))

res=x = 0
y = 1

print(x,y,end=" ")

for i in range (1, up_limit-1):


res += x+y
z = x + y
print (z,end=" ")
x = y
y = z
print("\n Sum of the Fibonacci series is ", res)

RESULT:
Thus, we have created a program to print the Fibonacci series and its sum.
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)

Enter the count of Fibonacci no : 20


0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
Sum of the Fibonacci series is 10944
>>>
MENU DRIVEN PROGRAM
8
Aim:
To write a menu-driven program to do the following by accepting an integer from the user.
Repeat the process as long as user says yes.
i. Print the given number is perfect no or not
ii. Print sum of odd position digits and even position digits present in the given
number separately
iii. Check whether the given number is palindrome or not
iv. Quit
Algorithm:
STEP 1: Open the python IDLE
STEP 2: Get the number n and assign ch = 1
STEP 3: Using the loop construct check for the choice ch is not 4 then goto step 4 or goto
step
STEP 4: Get the number n and choice ch
STEP 5: If ch = 1 then check for perfect number and print ‘Perfect number’ or print ‘Not a
perfect number’
STEP 6: If ch = 2 then add the odd position digits and even position digits of the number
and display.
STEP 7: If ch = 3 then reverse the given number and check the original number and
reverse number are equal. If equal print ‘Palindrome’ or ‘Not palindrome’.
STEP 8: If ch = 4 go to step 9
STEP 9: Stop

Expected input:
8128
Expected output:
Perfect number
9
10
Not palindrome
Code:
# PROGRAM 8
num = int(input("Enter any Number: "))
sum1 = 0
choice = 1
cnt = len(str(num))
eve = odd = 0
while choice != 4:
choice = int(input("Select your choice : \n 1. Find perfect number \
\n 2. Sum of odd and even position numbers \
\n 3. Number is a palindrome \n 4. Quit \
\n Enter your choice: "))
if choice == 1:
for i in range(1, num):
if(num % i == 0):
sum1 = sum1 + i

if (sum1 == num):
print(num, " is a Perfect Number")
else:
print(num, " is not a Perfect Number")
elif choice == 2:
Number = num
while(Number > 0):
Reminder = Number % 10
if (cnt % 2 == 0):
eve = eve + Reminder
else:
odd = odd + Reminder
Number = Number //10
cnt = cnt-1
print("\n Sum of the even position digits of Given Number is " , eve)
print("\n Sum of the odd position digits of Given Number is " , odd)
elif choice == 3:
rNumber = int(str(num)[::-1])
if rNumber == num:
print ("Given number's reverse is ",rNumber , ". So,it is a
palindrome")
else:
print ("Given number's reverse is ",rNumber , ". So, it is not a
palindrome")

RESULT:
Thus, we have created a menu driven program to find perfect number, sum of odd & even
digits and Palindrome or not.
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)

Enter any Number: 8128


Select your choice :
1. Find perfect number
2. Sum of odd and even position numbers
3. Number is a palindrome
4. Quit
Enter your choice: 1
8128 is a Perfect Number
Select your choice : …
Enter your choice: 2
Sum of the even position digits of Given Number is 9
Sum of the odd position digits of Given Number is 10
Select your choice : …
Enter your choice: 3
Given number's reverse is 8218 . So, it is not a palindrome
Select your choice : …
Enter your choice: 4
>>>
DISPLAY SECOND LARGEST NUMBER IN A LIST 9
Aim:
To write a program to display second largest element of a given list
Algorithm:
STEP 1: Open the python IDLE.
STEP 2: Get the list of numbers.
STEP 3: Declare maximum and minimum number of the first two elements of the list to
mx and smax.
STEP 4: Using the looping construct check second largest number and assign it to smax
STEP 5: Display smax
STEP 6: Stop
Expected input:
[69, 45, 23, 66, 34, 20, 12, 39]
Expected output:
66
Code:
# PROGRAM 9
lst = eval(input("Enter list : "))
mx=max(lst[0],lst[1])
smax=min(lst[0],lst[1])
n =len(lst)
for i in range(2,n):
if lst[i]>mx:
smax=mx
mx=lst[i]
elif lst[i]>smax and mx != lst[i]:
smax=lst[i]

print("Second largest number is : ", smax)

RESULT:
Thus, we have created a Python program to find the second largest number of the given
list.
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)

Enter list : [69,45,23,66,34,20,12,39]


Second largest number is : 66
FREQUENCY OF ELEMENT IN A LIST
10
Aim:
To write a Python program to display frequencies of all the element of a list.
Algorithm:
STEP 1: Open the python IDLE.
STEP 2: Get a list of numbers.
STEP 3: Assign the length of the list to lstlen and create an empty dictionary.
STEP 4: Using the looping construct take each element from the list and add the element
as key and its count as value to the dictionary.
STEP 5: Display the dictionary values
STEP 6: Stop
Expected input:
[2,4,2,3,5,6,7,4,5,6,3,3,2,4,6,3,8,5,6,4,2,4,5,6,7]
Expected output:
"2": 4,
"4": 5,
"3": 4,
"5": 4,
"6": 5,
"7": 2,
"8": 1
Code:
# PROGRAM 10
import json as j

lst = eval(input("Enter a list : "))


lstlen = len(lst)
lstdict ={}

for i in range(0,lstlen):
if lst[i] in lstdict.keys():
lstdict[lst[i]] = lstdict.get(lst[i])+1
else:
lstdict.setdefault(lst[i],1)

print ("Frequencies of all the element of the given list : ")


print(j.dumps(lstdict,indent = 3))
RESULT: Thus, we have created a Python program to display frequencies of all the
element of a list.
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)

Enter a list : [2,4,2,3,5,6,7,4,5,6,3,3,2,4,6,3,8,5,6,4,2,4,5,6,7]


Frequencies of all the element of the given list :
{
"2": 4,
"4": 5,
"3": 4,
"5": 4,
"6": 5,
"7": 2,
"8": 1
}
SUM OF NUMBERS ENDING WITH 3
11
Aim:
To write a Python program to find and display the sum of all numbers which are ending
with 3 from a list.
Algorithm:
STEP 1: Open the python IDLE.
STEP 2: Get a list of numbers.
STEP 3: Assign sumofno to 0.
STEP 4: Using the looping construct take each element from the list, check the number
ends with 3 and add the number with sumofno.
STEP 5: Display the sumofno
STEP 6: Stop

Expected input:
[23,45,36,33,83,90]
Expected output:
139

Code:
# PROGRAM 11
lst = eval(input ("Enter a list : "))
sumofno = 0
for no in lst:
if str(no)[-1]=='3':
sumofno += no

if sumofno > 0:
print ("Sum of numbers ending with 3 in the list",sumofno)
else:
print ("No number ending with 3")

RESULT: Thus, we have created a Python program to display the sum of all numbers
which are ending with 3 from a list.
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)

Enter a list : [23,45,36,33,83,90]

Sum of numbers ending with 3 in the list 139


SHIFTING NUMBERS OF A LIST 12
Aim:
To write a Python program to shift negative numbers to right and positive numbers to left.
Algorithm:
STEP 1: Open the python IDLE.
STEP 2: Get a list of numbers.
STEP 3: Assign length of the list to lenl2 and create list list2.
STEP 4: Using the looping construct move all positive value to the left and negative values
to the right side of the new list list2.
STEP 5: Display the list2
STEP 6: Stop

Expected input:
[-2,1,-3,-15,16,-17,5,-3,-6]
Expected output:
[1, 16, 5, -6, -3, -17, -15, -3, -2]

Code:
# PROGRAM 12
list1= eval (input("Enter a list of numbers : "))
lenl2=len(list1)
list2=[0] * lenl2
lenl2= lenl2-1
j=0
for i in range(lenl2+1):
if list1[i]>0:
list2[j]=list1[i]
j+=1
else:
list2[lenl2]=list1[i]
lenl2-=1

print(list2)
RESULT: Thus, we have created a Python program to shift negative numbers to right and
positive numbers to left.

OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)

Enter a list of numnbers : [-2,1,-3,-15,16,-17,5,-3,-6]


[1, 16, 5, -6, -3, -17, -15, -3, -2]`
DISPLAY NUMBER OF VOWELS , SPECIAL CHARACTERS 13
Aim:
To write a Python program to accept a line of text and do the following:
I. Print number of uppercase vowels
II. Print reverse of each word
III. Print number of special characters
Algorithm:
STEP 1: Open the python IDLE.
STEP 2: Read a string.
STEP 3: Assign up_char and s_char to 0 and create a list of words.
STEP 4: Using the looping construct extract each character to count upper case and
special characters.
STEP 5: Using another looping construct extract each word and prints the reverse word.
STEP 6: Print the number of upper case and special character.
STEP 7: Stop

Expected input:
BELIEVE you CAN.
Expected output:
EVEILEB uoy .NAC
5
1

Code:
# PROGRAM 13
line_str = input("Enter a line of Text : ")

up_char=s_char=0
words = line_str.split()

for char in line_str:


if char in 'AEIOU':
up_char+=1
if char.isalnum() == False and char.isspace()==False:
s_char +=1
print ("Reversal of each word is ", end = " ")
for word in words:
print (word[::-1],end = " ")
print()
print("Number of Upper case letters in the given string is ",up_char)
print("Number of Special characters in the given string is ",s_char)

RESULT: Thus, we have created a Python program to count and display the upper case
letters, special characters and reverse of each word.

OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)

Enter a line of Text : BELIEVE you CAN. You're done Half-way there.
Reversal of each word is EVEILEB uoy .NAC er'uoY enod yaw-flaH .ereht
Number of Upper case letters in the given string is 5
Number of Special characters in the given string is 4
REPLACE CHARACTERS 14
Aim:
To write a Python program to accept a line of string and a character and find the
occurrence of the given characters, if the character is in uppercase, double it otherwise
replace it with ‘#’ and swap first letter and last letter of each word present in the string.
Also, print each word along with length of each word, number of replacements occurred.

Algorithm:
STEP 1: Open the python IDLE.
STEP 2: Read a string and a character and split the words and store as list.
STEP 3: Using the looping construct find the occurrence of the character.
STEP 4: Using the looping construct extract each word, if the character is in uppercase,
double it otherwise replace it with ‘#’ and swap first letter and last letter of the word. Also
calculate the replacements
STEP 5: Print the number of word, length of the word and number of replacement.
STEP 6: Stop

Expected input:
Black bull in Bubble
b
Expected output:
kBlacB 2 6
lul# 2 4
ni 1 2
eBu##lB 4 7

Code:
# PROGRAM 14
for word in words:
replace_char=0
new_word=''
for char in word:
if char.lower() == find_char.lower():
fcount_char+=1
if char.isupper() and char.lower() == find_char.lower():
new_word=new_word + char*2
replace_char+=1
elif char.islower() and char.lower() == find_char.lower():
new_word=new_word + '#'
replace_char+=1
else:
new_word=new_word + char
lst_new_words.append([new_word,replace_char,len(new_word)])

cnt=0
for word_lst in lst_new_words:
word = word_lst[0]
swap_word = word[-1]+word[1:-1]+word[0]
lst_new_words[cnt][0]=swap_word
lst_new_words[cnt][1] = lst_new_words[cnt][1] + 1 # As swaping is a
replacement
cnt +=1
print ("New Word No of Replacement Length of string")
for details in lst_new_words:
print (details[0],'\t\t' , details[1],'\t\t',details[2])

RESULT: Thus, we have created a Python program to replace the text and display the
same.
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)

Enter a line of Text : Black bull in Bubble


Enter a character to be searched : b
[['BBlack', 1, 6], ['#ull', 1, 4], ['in', 0, 2], ['BBu##le', 3, 7]]
New Word No of Replacement Length of string
kBlacB 2 6
lul# 2 4
ni 1 2
eBu##lB 4 7
MAXIMUM & MINIMUM VALUE IN LIST 15
Aim:
To write a Python program to input ‘n’ employees salary and find minimum and maximum
salary in the list of salaries.

Algorithm:
STEP 1: Open the python IDLE.
STEP 2: Read n the number of employees.
STEP 3: Using the looping construct get the salary of employees and store in a list.
STEP 4: Using the max and min function find the minimum and maximum salary.
STEP 5: Print the maximum and minimum salary.
STEP 6: Stop

Expected input:
10
12400,12000,23000,45090,9100,4500,64000,19000,17000,13500
Expected output:
45090
4500
Code:
# PROGRAM 15
no_emp=int(input("Enter the number of Employees : "))
emp_lst = []
for i in range(no_emp):
sal = float(input("Enter the salary : "))
emp_lst.append(sal)

print ("The maximum salary in the employee list is " , max( emp_lst))
print ("The minimum salary in the employee list is " , min( emp_lst))

RESULT: Thus, we have created a Python program to display the maximum and
minimum value in a list
OUTPUT: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL ALONG WITH THE BOX)

Enter the number of Employees : 5


Enter the salary : 12400
Enter the salary : 12000
Enter the salary : 45090
Enter the salary : 23000
Enter the salary : 4500
The maximum salary in the employee list is 45090.0
The minimum salary in the employee list is 4500.0

You might also like