Professional Documents
Culture Documents
LAB MANUAL
PREPARED BY
Year / Sem : II / IV
Subject Name/Code: 18CSC207J/ ADVANCED PROGRAMMING PRACTICE LAB
1.
Ex.No: 1
Date: Identifying appropriate data types, variables and simple
programs to understand the basic program structure
Aim:
To Write a Python Programs to
i) Display the Fibonacci sequence up to n-th term
ii) Test whether a given number is Even or Odd
iii) Select the game that we like using elif statement
iv) Write the way of accessing list elements using different loops
Algorithm 1:
1. Start the program
2. Get the input, no.of terms from the user
3. Check whether we have entered positive integer
4. Calculate the fibonocci sequence
5. Print the sequence
6. Stop
Algorithm2:
Algorithm 3:
Algorithm 4:
Program 1:
Program 2:
# Program to check the given number is odd or even
num = int(input('Enter any number : '))
if num % 2 == 0:
print(f'The number {num} is a Even number')
else:
print(f'The number {num} is a Odd number')
Output:
Enter a number: 3
3
3 is Odd
Program 3:
#Program to choose and display the game
choice = input(f'Which game do you like, Press\nC - Cricket\nH - Hokey: ')
if choice == 'C':
print('You are a Cricketer!')
elif choice == 'H':
print('You are a Hockey player!')
else:
print('You are not interested in Sports')
Output:
Which game do you like, Press\nC - Cricket\nH - Hokey: H
You are a Hockey player
Program 4:
# Program to access the values from list
# Accessing items using while loop
cars =["Aston", "Audi", "McLaren"]
i =0
while(i < len(cars)):
print(cars[i])
i += 1
Output:
(0, ’Aston’)
(1, ‘Audi’)
(2, ‘McLaren’)
Result: Thus the Python program for Fibonacci series, Even or Odd , selecting Game using elif,
accessing list of elements using iteration statements and Factorial of a given number are
executed successfully.
Ex.No: 2
Date: Programs to implement Procedural Programming Paradigms
Aim:
To Write a Python Programs to
i) Find area of square, rectangle and triangle
ii) Find the factorial of a number using procedure
iii) Swap two numbers using functions
iv) Find the absolute value of given numbers
v) Perform mathematical operations using procedure.
Algorithm 1:
1. Start the program
2. Write areasquare() to calculate area of square
3. Write arearect() to calculate area of rectangle
4. Write areatri() to calculate area of triangle
5. Print area of square, rectangle and triangle.
6. Stop
Algorithm 2:
1. Start the program
2. Initialize the number to find factorial
3. Write calc_factorial() to find factorial
4. Return the result
5. Display
Algorithm 3:
1. Start the program
2. Initialize the numbers
3. Write swap function to swap.
4. Return the result
5. Display
Algorithm 4:
1. Start the program
2. Write absolute_value() to find absolute value.
3. Check the value is greater than or equal to zero.
4. Return the result
5. Display
Algorithm 5:
1. Start the program
2. Initialize the numbers
3. Write add function to find addition.
4. Write multi function to find multiplication.
5. Write div function to find division.
6. Write sub function to find subtraction.
7. Return the result
8. Display
Program 1:
#Function to find area of square
def areasquare(x):
return x*x
#Function to find area of rectangle
def arearect(a,b):
return a*b
#Function to find area of triangle
def areatri(a,b):
return (a*b)/2
print(areasquare(4))
print(arearect(3,4))
print(areatri(5,6))
Output:
16
12
15.0
Program 2:
if x == 1:
return 1
else:
return (x * calc_factorial(x-1))
num = 4
print("The factorial of", num, "is", calc_factorial(num))
Output:
The factorial of 4 is 24
Program 3:
# Program to swap two numbers
Output:
2
3
Program 4:
# Program to find absolute value of given numbers
def absolute_value(num):
"""This function returns the absolute
value of the entered number"""
if num >= 0:
return num
else:
return -num
print(absolute_value(2))
print(absolute_value(-4))
Output:
2
4
Program 5:
# Program to get x, y numbers from the user and write four functions add, subtract, division and
muliplication function to perform arithmetic operations.
def add_numbers(x,y):
sum = x + y
return sum
def multi_numbers(x,y):
sum = x - y
return sum
def div_numbers(x,y):
sum = x * y
return sum
def sub_numbers(x,y):
sum = x / y
return sum
num1 = 5
num2 = 6
print("The sum is", add_numbers(num1, num2))
print("The multiplication is", multi_numbers(num1, num2))
print("The division is", div_numbers(num1, num2))
print("The subtraction is", sub_numbers(num1, num2))
Output:
The sum is 11
The multiplication is 30
The division is 8.16
The subtraction is -1
Result: Thus the Python program for area of square,rectangle and triangle , factorial of a
number, swapping two numbers, finding absolute number, addition, subtraction, multiplication
and division using function are executed successfully.