You are on page 1of 16

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY

Ramapuram, Chennai- 600089.

FACULTY OF ENGINEERING AND TECHNOLOGY


Department of Computer Science & Engineering

LAB MANUAL

18CSC207J/ ADVANCED PROGRAMMING PRACTICE LAB

CLASS : B.Tech. [U.G]


YEAR / SEM. : II Year / IV Semester
SOFTWARE REQUIREMENT : Python 3.7

PREPARED BY

S.No. Staff Name Designation Signature


1
2
3
4
5
6
7
8
9
Approved By
HOD/CSE

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY


RAMAPURAM
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Year / Sem : II / IV
Subject Name/Code: 18CSC207J/ ADVANCED PROGRAMMING PRACTICE LAB

LIST OF EXPERIMENTS (30 Hours)

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:

1. Start the program


2. Get the input from the user
3. Check the given number is odd or even
4. Print the number is odd or even
5. Stop

Algorithm 3:

1. Start the program


2. Get the input from the user
3. Check the correct game
4. Display the game.
5. Stop

Algorithm 4:

1. Start the program


2. Initialize the list consisting of Aston, Audi, McLaren.
3. Display the list using while loop, For each, For and Enumerate
4. Stop

Program 1:

# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Output:

How many terms? 7


Fibonacci Sequence:
0
1
1
2
3
5
8

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

# Accessing items using for-in loop


  
cars =["Aston", "Audi", "McLaren"]
for x in cars:
    printx
    i +=1
Output:
Aston
Audi
McLearn

# Accessing items and indexes enumerate()


  
cars = ["Aston" , "Audi", "McLaren "]
for x in enumerate(cars):
    print (x[0], x[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:

# To find the factorial of a number


def calc_factorial(x):
"""This is a recursive function
to find the factorial of an integer"""

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

def swap(x, y):


    temp = x;
    x = y;
    y = temp;
  
# Driver code
x=2
y=3
swap(x, y)
print(x)
print(y)

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.

You might also like