PROGRAM - 1
AIM:
To write a menu driven program to determine whether a
number us Armstrong or not and create a Fibonacci series
ALGORITHM:
1. Start
2. Define a function Armstrong with num as its only
parameter
3. Inside the function create a variable str which holds the
value str(num)and another variable l which holds the
length of str
4. Create a new variable , n which holds the value 0.
5. Using for loop with range l, iterate for l items and for
each iteration l items and for each multiply l with the
integer value of the corresponding string from str.
6. Check if num=n and if it is true print num is an Armstrong
number, else print it is not a Armstrong number and
hence end the function Armstrong
7. Define another function fbs with n as its only parameter
8. Assign the variables n1, n2 with the values 0,1
respectively and print those numbers too
9. Using for loop iterate for n times and for each iteration
assign a variable n3 with the sum of n1,n2 and assign the
value of n2 to n1 and n3 to n2.
10. Print n3 for each iteration and hence end the
function fbs
11. Print “1.Armstrong number “ and “2) Fibonacci
series”
12. Input a integer a
13. If a equals 1 call the function Armstrong (a) , and if a
equals 2 call the function fbs(a), else print invalid input
14. stop
RESULT:
Thus, a menu driven program to determine whether a number
is Armstrong or not and to create a Fibonacci series is
executed successfully
PROGRAM - 2
AIM:
To write a program that contain user defined functions to
calculate area , perimeter or surface area which is applicable
for various shapes like square, rectangle , triangle , circle and
cylinder.
ALGORITHM:
1. Start
2. Define a function sqar(s) with side of a square as a
parameter which prints the area of square , s*s
3. Define a function sqpm (s) with side of a square as a
parameter which prints the perimeter of square , 4*s
4. Define a function recar(l,b) with length, breath of a
rectangle as a parameter which prints the area of
rectangle , l*b
5. Define a function recpm(l,b) with length, breath of a
rectangle as a parameter which prints the perimeter of
rectangle , 2*(l+b)
6. Define a function crar(r) with radius of a circle as a
parameter which prints the area of circle ,(22/7)*r**2
7. Define a function crpm(r) with radius of a circle as a
parameter which prints the perimeter of circle ,
(22/7)*r*2
8. Define a function triar(h,b) with height, breath of a
rectangle as a parameter which prints the area of
triangle , (1/2)*h*b
9. Define a function tripm(s1,s2,s3) with sides of a triangle
as a parameter which prints the perimeter of triangle,
s1+s2+s3
10. Define a function cyar (h,r) with radius, height of a
cylinder as a parameter which prints the surface area of
cylinder , 2*(22/7)*r*(r+h)
11. Print area, perimeter as a string with its
corresponding integers 1,2
12. Print the names of the shapes as a string with its
corresponding integers 1,2 ,3,4,5
13. Input a integer from each and use if..elif.. else block
to input the corresponding argument(s) and call the
functions accordingly
14. stop
RESULT:
Thus , the python program to calculate area , perimeter or
surface area which is applicable for various shapes like
square, rectangle , triangle , circle and cylinder is executed
successfully.
Program -3
Aim:
To write a python to perform all the list manipulation using
functions
ALGOTITHM:
1. start
2. input a list using eval function , l
3. print all the functions along with its corresponding
integer values from 1-9
4. input an integer from 1-9 to choose the manipulation
function, n
5. if n=1 print the length of the function using len(l) and
print () functions
6. input a element ,ele if n in (2,3,4,5,7)
7. if n=2 print the count the element , ele in the list ,l using
count (ele) and print functions
8. if n=3 print the index the element , ele in the list ,l using
index (ele) and print functions
9. if n=4 using append (ele ) add an element to the list ,l
10. if n=5 input another list l1 and add it to the list ,l
using extend (l1) and print the list ,l
11. if n=6 input an index ,i and use the function insert
(I,ele) to add the element , ele in the index ,i and print
the list, l
12. if n=7 using the remove(ele) , remove the element ,
ele from the list ,l and print the list , l
13. if n=8 , input an index , i and using pop (i) remove
the element at the index i and print the list ,l using print
function
14. if n=9 , print “exit”
15. stop
RESULT:
Thus, the python program to perform all list manipulations
using function are executed successfully.
PROGRAM - 4
AIM:
To write a python program to generate a random number
between 1-10
ALGORITHM:
1. Start
2. Import random module
3. Generate a random integer from 1-10 using random.
Randint(1,10) and store it in a variable num1
4. Store the value of 0 to the variable , points
5. Using for loop with variable i and range from 0 to 3 input
integer from the user , n
6. For each iteration check whether n= num1 , if true , then
add 100 to points and prints “you won!”
7. Or if i <2 print “ you have 2-i more chance”
8. Else print “ you lost!”
9. Stop
RESULT
Thus, the python program to generate a random number
between 1-10 is executed successfully.
PROGRAM - 5
AIM:
To write a user define function which reads the text from a
text file and displays each word separated by a “ #” in
another
ALGORITHM:
1. Start
2. Define a function readsep()
3. Crate the variables fb , data to open and store the lines
generated by the readlines functions respectively.
4. Crate a variable newlist to store a empty list
5. Using for loop with variable i in data create a new
variable sep that stores the value given by the function
i.split()
6. Now join “#” between each word using join function
7. Add a “ \n” at the end of sep and add sep to newlist
8. C lose the file variable fb out of the loop
9. Now open an empty text file using the open function and
store it in the variable fb1
10. Write all the data from the list, newlist to the file
operator, fb using the function writelines().
11. Close fb1 and open the same text file using another
variable fb2 in read more
12. Using readlines() get all the data in the file as a list
and print each line using a for loop
13. Close the file variable , fb2
14. Stop
RESULT:
Thus, the user define function which reads the text from a text
file and display each word separated by a “#” is executed
successfully.