Lab Manual: Programming and Problem Solving LAB
Lab Manual: Programming and Problem Solving LAB
PUNE-411048
LAB MANUAL
Programming and Problem Solving LAB
FE (2019 PAT)
A.Y.2021-22
SEM-I
INDEX
Page 1
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
PPS LABORATORY
Sr. No Group-A
1 To calculate salary of an employee given his basic pay (take as input from user).
Calculate gross salary of employee. Let HRA be 10 % of basic pay and TA be 5%
of basic pay. Let employee pay professional tax as 2% of total salary. Calculate
net salary payable after deductions
2 To accept N numbers from user. Compute and display maximum in list, minimum
in list, sum and average of numbers
3 To accept student’s five courses marks and compute his/her result. Student is
passing if he/she scores marks equal to and above 40 in each course. If student
scores aggregate greater than 75%, then the grade is distinction. If aggregate is
60>= and <75 then the grade if first division. If aggregate is 50>= and <60, then
the grade is second division. If aggregate is 40>= and <50, then the grade is third
division.
4 To simulate simple calculator that performs basic tasks such as addition,
subtraction, multiplication and division with special operations like computing
x^y and x!.
5 To accept the number and Compute a) square root of number, b) Square of
number, c) Cube of number d) check for prime, d) factorial of number e) prime
factors
6 To accept two numbers from user and compute smallest divisor and Greatest
Common Divisor of these two numbers.
7 Write a python program that accepts a string from user and perform following
string operations- i. Calculate length of string ii. String reversal iii. Equality check
of two strings iii. Check palindrome ii. Check substring
8 Create class EMPLOYEE for storing details (Name, Designation, gender, Date of
Joining and Salary). Define function members to compute a)total number of
employees in an organization b) count of male and female employee c) Employee
with salary more than 10,000 d) Employee with designation “Asst Manager”
Mini Project
Page 2
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
ASSIGNMENT. NO.01
Title:
To calculate salary of an employee given his basic pay (take as input from user). Calculate
salary of employee. Let HRA be 10 % of basic pay and TA be 5% of basic pay. Let employee
pay professional tax as 2% of total salary. Calculate salary payable afterdeductions.
Objective:
Problem Statement:
Outcomes:
1. Students will be able to demonstrate calculations of employeesalary.
2. Students will be able to demonstrate different Operator &formulas.
3. Students will be able to demonstrate different Operations on Available data of
employeesalary.
Theory:
Python Basics:
Python is an interpreted, high-level, general-purpose programming language. Created by
Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code
readability with its notable use of significant whitespace.
What is Basic salary?
Basic salary is the base income of an individual. It is a fixed part of one's compensation
package.
A basic salary depends on the employee’s designation and also the industry in which the
employee works.
Basic salary is the amount paid to an employee before any extras are added or taken off, such
as reductions because of salary sacrifice schemes or an increase due to overtime or a bonus.
Allowances, such as internet for home-based workers or contributions to phone usage, would
also be added to the basicsalary.
Page 3
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
Gross salary:
Gross salary is the amount calculated by adding up one's basic salary and allowances, before
deduction of taxes and other deductions. It includes bonuses, over-time pay, holiday pay, and
other differentials.
Gross Salary = Basic Salary + HRA + Other Allowances
Allowances:
An allowance is an amount received by the employee for meeting service requirements.
Allowances are provided in addition to the basic salary and vary from company to company.
Some common types of allowances are shown below:
HRA:
HRA received is not fully exempt from tax. HRA that you can claim is the lowest of the
following:
The total amount received as the HRA from the employer in the financialyear.
Actual rent paid in the year – 10% of the basic salary in the year.
50% of the annual basic salary if staying in a metro city or 40% of the annual basic
salary if staying in a non-metrocity.
Sample Example:
Python program to get employee wages and number of days worked from user and find Basic
Pay, DA, HRA, PF and Net Pay.
(Note HRA, DA and PF are 10%,5%and 12% of basicpay respectively.)
Sample Input 1:
300
30
Sample Output
1: Basic
Pay:3000
Page 4
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
DA: 150
HRA:300
PF:360
Net Pay: 3090
Conclusion:
Thus, we have successfully understood concept of salary calculation formulas and performed
salary calculation in python.
Page 5
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
ASSIGNMENT. NO.02
Title:
To accept N numbers from user. Compute and display maximum in list, minimum in
list, sum and average of numbers.
Objective:
Understand basic concepts of python programming language and try to solve list concepts
related programs.
Problem Statement:
Outcomes:
Theory:
What is list?
Python offers a range of compound datatypes often referred to as sequences. List is one of the
most frequently used and very versatile datatype used in Python.
It can have any number of items and they may be of different types (integer, float, string etc.).
Page 6
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
Syntax:
# empty
list my_list
= []
# list of integers
my_list = [1, 2,
3]
# list with mixed
datatypesmy_list = [1,
"Hello", 3.4]
Also, a list can even have another list as an item. This is called nested
list. # nested list
my_list = ["mouse", [8, 4, 6], ['a']]
List Index
We can use the index operator [] to access an item in a list. Index starts from 0. So, a list having
5 elements will have index from 0 to 4.
Trying to access an element other that this will raise an IndexError. The index must be an
integer. We can't use float or other types, this will result into TypeError.
Nested list are accessed using nested
indexing. my_list = ['p','r','o','b','e']
# Output: p
print(my_list[0]
) # Output: o
print(my_list[2]
) # Output: e
print(my_list[4]
indexing # my_list[4.0]
Page 7
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
# Nested List
n_list = ["Happy",
[2,0,1,5]] # Nested
indexing
# Output: a
print(n_list[0]
[1])
# Output: 5print(n_list[1][3])
Fig: ListIndexing
In this experiment, we will learn how to find the maximum and minimum number in a python
list. Python list can hold items of any data types. All items are separated by a comma and
placed inside a square bracket. We can access any item by using its index. The index starts at
0. The index for the first element is 0, the index of the second element is 1etc.
Here will show you how to find the maximum and minimum number in a list using a loop. All
the numbers will be entered by the user. The user will enter the list items one by one and our
program will read them.
First, we will ask the user to enter the total number count. Then using a for loop, we will read
each number and append them to the list. Finally, again using one more for loop, we will
calculate the maximum and minimum number and print out the result. Let’s take a look into
the following sample program first.
Explanation :
1. Create one empty list my_list. We are using one empty square bracket to create the empty
Page 8
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
2. Get the total number of elements the user is going to enter and save it in the count variable.
This is required because we will ask the user to enter each number one by one. If the value of
count is 4, the user will have to enter four numbers to add to thelist.
3. Usingaforloop,getthenumbersandappendittothelistmy_list.Forappendinganumber append
method is used. This method takes the value we are adding as the parameter. For readingthe value,
we are using the input method. This method will read the value from the user. The return value of
this method is of string type. Wrapping it as int() will convert the entered value to an integer.
4. Print the list to theuser.
5. Create two variables to hold the minimum and maximum number. We are assigning the
first element of the list to both of these variables first. We will update these variables on the
next step. On this step, we are assuming that the minimum and maximum value of the list is
equal to the first element of the list. We will compare this value with all other elements of the
list one by one and update them ifrequired.
6. Run one for loop on the list again. For each number, check if it is less than the minimum
number. If yes, assign the minimum value holding variable to this number. Similarly, update
the maximum value if the number is more than the currentmaximum.
7. After the list is completed reading, print out both maximum and minimumnumbers.
Algorithm:
1.Create an empty list
named l 2.Read the value of
n
3.Read the elements of the list until
n 4.Assign l[0] as maxno
5.If l[i]>maxno then set
maxno=l[i] 6.Increment i by 1
7. Repeat steps 5-6 untili<n
8. Print the value of maximumnumber
Page 9
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
ASSIGNMENT. NO.03
Title:
To accept student’s five courses marks and compute his/her result. Student is passing if he/she
scores marks equal to and above 40 in each course. If student scores aggregate greater than
75%, then the grade is distinction. If aggregate is 60>= and <75 then the grade if first division.
If aggregate is 50>= and <60, then the grade is second division. If aggregate is 40>= and <50,
then the grade is thirddivision
Objective:
Problem Statement:
Outcomes:
2. Students will be able to demonstrate different Operator & formulas for grade andsum.
Theory:
In this assignment user has to enter five different marks for five subjects. Next, it will find the
Total, average, and Percentage of those Five Subjects. For this, we are using the arithmetic
operator to perform arithmetic operations.
Problem Description
The program takes in the marks of 5 subjects and displays the grade.
Problem Solution
1. Take in the marks of 5 subjects from the user and store it in differentvariables.
2. Find the average of themarks.
Page 10
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
3. Use an else condition to decide the grade based on the average of themarks.
4. Exit.
Following flowchart will show basic idea about sum,average calculation in python , same
logic we can apply fow percentage calculation.
Conclusion: Thus, we have performed how to calculate percentage and grade, average in
python.
Page 11
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
ASSIGNMENT. NO.04
Title:
To accept the number and Compute a) square root of number, b) Square of number, c) Cube
of number d) check for prime, d) factorial of number e) prime factors
Objective:
Problem Statement:
Outcomes:
3. Students will be able to demonstrate different Operations on any integer by using ownlogic.
Theory:
To understand this experiment, you should have the knowledge of following Python
programmingtopics:
Python Input, Output and Import
Python provides numerous built-in functionsthat are readily available to us at the Python
prompt.
Some of the functions like input() and print() are widely used for standard input and output
operations respectively. Let us see the output section first.
Python Output Using print() function
We use the print() function to output data to the standard output device (screen).
We can also output data to a file, but this will be discussed later. An example use is given
below. print('This sentence is output to the screen')
Page 12
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
Python Numbers
Integers, floating point numbers and complex numbers falls under Python numberscategory.
They are defined as int, float and complex class in Python.
We can use the type() function to know which class a variable or a value belongs to and the
isinstance() function to check if an object belongs to a particular class.
a=5
print(a, "is of type",
type(a)) a = 2.0
print(a, "is of type",
type(a)) a = 1+2j
print(a, "is complex number?", isinstance(1+2j,complex))
Python Operators
Operators are special symbols in Python that carry out arithmetic or logical computation. The
value that the operator operates on is called the operand.
For example:
>>>2+35
Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of
the operation.
sqrt() function is an inbuilt function in Python programming language that returns the
square root of any number.
Syntax:
math.sqrt(x)
Parameter:
Page 13
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
Returns:
It returns the square root of the number passed in the parameter.
Example:
Square of a Number
In this article, we will show you, How to write a PythonProgram to Calculate Square of a
Number using Arithmetic Operators, and Functions with example.
allows the user to enter any numerical value. Next, it will finds the square of that number
using Arithmetic Operator.
Example:
# Python Program to Calculate Square of a Number
number = float(input(" Please Enter any numeric Value
: ")) square = number * number
print("The Square of a Given Number {0} = {1}".format(number, square))
Cube of a Number
we will show you, How to write a PythonProgram to Calculate Cube of a Number using
Arithmetic Operators, and Functions with example.
Example:
# Python Program to Calculate Cube of a Number
number = float(input(" Please Enter any numeric Value :
")) cube = number * number * number
print("The Cube of a Given Number {0} = {1}".format(number, cube))
Page 14
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
Prime Number:
A positive integer greater than 1 which has no other factors except 1 and the number itself is
called a prime number. 2, 3, 5, 7 etc. are prime numbers as they do not have any other factors.
But 6 is not prime (it is composite) since, 2 x 3 =6.
Given a positive integer N. The task is to write a Python program to check if the number is
primeor not.
Definition: A prime number is a natural number greater than 1 that has no positive
divisorsother than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11,….}.
Examples :
nput: n = 11
Output: true
Input: n = 15
Output:false
Input: n = 1
Output:false
The idea to solve this problem is to iterate through all the numbers starting from 2 to (N/2)
using a for loop and for every number check if it divides N. If we find any number that
divides, we return false. If we did not find any number between 2 and N/2 which divides N
then it means that N is prime and we will return True.
Below is the Python program to check if a number is prime:
num = 11
# If given number is greater
than 1 if num> 1:
# Iterate from 2 to n / 2
for i in range(2,
num//2):
Factorial Number:
The factorial of a number is the product of all the integers from 1 to that number.
For example, the factorial of 6 (denoted as 6!) is 1*2*3*4*5*6 = 720. Factorial is not
defined for negative numbers and the factorial of zero is one, 0! = 1.
factorial() in Python
Not many people know, but python offers a direct function that can compute the factorial of a
number without writing the whole code for computing factorial.
Naive method to compute factorial
Conclusion: Thus , in this experiment we have studied and performed mathematical operation
on given number.
Page 16
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
ASSIGNMENT. NO.09
Title:
Write a python program that accepts a string from user and perform following string
operations- i. Calculate length of string ii. String reversal iii.Equality check of two strings iii.
Check palindrome
ii. Check substring
Objective:
Problem Statement:
3. Students will be able to demonstrate different Operations on any string by using ownlogic.
Theory:
A string is a list of characters in order. A character is anything you can type on the keyboard
in one keystroke, like a letter, a number, or a backslash. Strings can have spaces: "hello
world". An empty string is a string that has 0 characters.
Page 17
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
StringManipulation
>>> print
word Hello
World
Accessing
letter=word[0]
>>>print
letter H
Length
>>>len(word)
11
Finding
Count
Page 18
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
Slicing
Keep in mind that python, as many other languages, starts to count from 0!!
Split Strings
Startswith / Endswith
>>>word.startswith("H
") True
>>>word.endswith("d
") True
>>>word.endswith("w
") False
Changing Upper and Lower Case Strings
Page 19
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)
HELLO WORLD
>>> print
string.lower() hello
world
>>>
printstring.title()
Hello World
>>>
printstring.capitalize()
Helloworld
>>> print
string.swapcase()
hELLOwORLD
Example: Let the input string be “i like this program very much”. The function should
changethe string to “much very program this likei”
Input: hello
Output:
olleh
The format() method that is available with the string object is very versatile and powerful
in formatting strings. Format strings contains curly braces {} as placeholders or replacement
fields which gets replaced.
We can use positional arguments or keyword arguments to specify theorder.
The format() method can have optional format specifications. They are separated from
field nameusingcolon.Forexample,wecan left-justify <,right-
justify>orcenter^astringinthegiven space. We can also format integers as binary, hexadecimal
etc. and floats can be rounded or displayed in the exponent format. There are a ton of
formatting you canuse.
Conclusion:
Thus, in this experiment we have studied and performed string operation successfully.
Page 20