0% found this document useful (0 votes)
337 views20 pages

Lab Manual: Programming and Problem Solving LAB

The document provides information about the Programming and Problem Solving Lab at Trinity Academy of Engineering in Pune for the academic year 2021-2022. It includes the teaching scheme, examination scheme, and credits for the practical course. It also lists 7 assignments that involve writing Python programs to solve problems related to salary calculations, finding maximum/minimum/sum/average of a list of numbers, student result calculation, simple calculator operations, number computations, greatest common divisor, and string operations. Finally, it provides a sample assignment on salary calculation that demonstrates concepts like basic salary, gross salary, allowances like HRA and formulas used in Python.

Uploaded by

Advait Keskar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
337 views20 pages

Lab Manual: Programming and Problem Solving LAB

The document provides information about the Programming and Problem Solving Lab at Trinity Academy of Engineering in Pune for the academic year 2021-2022. It includes the teaching scheme, examination scheme, and credits for the practical course. It also lists 7 assignments that involve writing Python programs to solve problems related to salary calculations, finding maximum/minimum/sum/average of a list of numbers, student result calculation, simple calculator operations, number computations, greatest common divisor, and string operations. Finally, it provides a sample assignment on salary calculation that demonstrates concepts like basic salary, gross salary, allowances like HRA and formulas used in Python.

Uploaded by

Advait Keskar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)

KJ’s Educational Institute’s


TRINITY ACADEMY OF ENGINEERING

PUNE-411048

Department of Computer Engineering

LAB MANUAL
Programming and Problem Solving LAB
FE (2019 PAT)

A.Y.2021-22
SEM-I

Teaching Scheme: Examination Scheme: Credit:

Practical: 4Hrs/ Week Term Work: 25 Marks Prac: 2


Practical: 50 Marks

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:

Understand basic concepts of programming language and try to solve mathematical


calculations in programming approach with the help of formulas and equations.

Problem Statement:

To Calculate salary payable of employee

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.

Hardware Requirement: Any CPU with i3 Processor or similar, 1 GB RAM or more,2 GB


Hard Disk or more

Software Requirements: 32/64 bit Linux(Ubuntu/Fedora)Operating System, latest any Python


Tool Like Anaconda,Pycharm, Python IDLE, Python Atom,Eclipse.

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 or House Rent Allowance: It is an amount paid out to employees by companies


for expenses related to rentedaccommodation.
 Leave Travel Allowance (LTA): LTA is the amount provided by the company to cover
domestic travel expenses of an employee. It does not include the expenses for food,
accommodation, etc. during the travel.
 Conveyance Allowance: This allowance is provided to employees to meet travel
expenses from residence towork.
 Dearness Allowance: DA is a living allowance paid to employees to tackle the effects of
inflation. It is applicable to government employees, public sector employees, and
pensioners only.
 Other such allowances are the special allowance, medical allowance, incentives,etc.

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:

To Find maximum and minimum number, sum,average from given list.

Outcomes:

1. Students will be able to understand basic concept of list inpython.

2. Students will be able to demonstrate different Operator & formulas on givenlist.

3. Students will be able to demonstrate different types of list exampleseasily.

Hardware Requirement: Any CPU with i3 Processor or similar, 1 GB RAM or more,2 GB


Hard Disk or more

Software Requirements: 32/64 bit Linux(Ubuntu/Fedora)Operating System, latest any


Python Tool Like Anaconda,Pycharm, Python IDLE, Python Atom,Eclipse.

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.

How to create a list?


In Python programming, a list is created by placing all the items (elements) inside a square

bracket [ ], separated by commas.

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']]

How to access elements from a list?


There are various ways in which we can access the elements of a list.

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]

# Error! Only integer can be used for

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)

list. This is a list without anyitems.

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

Conclusion: Thus, we have studied and performed operation on list in python.

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:

Understand basic concepts of programming language and try to solve mathematical


calculations in programming approach with the help of formulas and equations.

Problem Statement:

To calculate percentage,aggregate of given five subjects in python.

Outcomes:

1. Students will be able to understand basic concept of percentage calculation inpython.

2. Students will be able to demonstrate different Operator & formulas for grade andsum.

3. Students will be able to demonstrate different Operations by using ownlogic.

Hardware Requirement: Any CPU with i3 Processor or similar, 1 GB RAM or more,2 GB


Hard Disk or more

Software Requirements: 32/64 bit Linux(Ubuntu/Fedora)Operating System, latest any


Python Tool Like Anaconda,Pycharm, Python IDLE, Python Atom,Eclipse.

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.

Fig: Flowchart for underating average, sum 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:

Understand basic concepts of programming language and try to solve mathematical


calculations in programming approach with the help of formulas and equations.

Problem Statement:

To perform number different operation on given number in python.

Outcomes:

1. Students will be able to understand basic concept of number operationslogic.

2. Students will be able to demonstrate different integer number for identifying


numberoperations.

3. Students will be able to demonstrate different Operations on any integer by using ownlogic.

Hardware Requirement: Any CPU with i3 Processor or similar, 1 GB RAM or more,2 GB


Hard Disk or more

Software Requirements: 32/64 bit Linux(Ubuntu/Fedora)Operating System, latest any


Python Tool Like Anaconda,Pycharm, Python IDLE, Python Atom,Eclipse.

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)

# Output: This sentence is output to the screen


a=5
print('The value of a is', a)
# Output: The value of a is 5
Python Data Types
Every value in Python has a datatype. Since everything is an object in Python
programming, data types are actually classes and variables are instance (object) of these
classes.
There are various data types in Python. Some of the important types are listed below.

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.

Python math function | sqrt()

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)

x is any number such that x>=0

Returns:
It returns the square root of the number passed in the parameter.
Example:

# Python3 program to demonstrate


the # sqrt() method
# import the math
module import math
# print the square root of 0
print(math.sqrt(0))
# print the square root
of 4 print(math.sqrt(4))
# print the square root of
3.5 print(math.sqrt(3.5))

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:

# Python program to check


if # given number is prime
or not

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):

# Ifnum is divisible by any number


between # 2 and n / 2, it is not prime
if (num % i) == 0:
print(num, "is not a prime
number") break
else:
print(num, "is a prime
number") else:
print(num, "is not a prime number")
Page 15
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)

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:

Understand basic concepts of programming language and try to solve mathematical


calculations in programming approach with the help of formulas and equations.

Problem Statement:

To perform String operation in python.


Outcomes:

1. Students will be able to understand basic concept of string inpython.

2. Students will be able to demonstrate different operations onstring.

3. Students will be able to demonstrate different Operations on any string by using ownlogic.

Hardware Requirement: Any CPU with i3 Processor or similar, 1 GB RAM or more,2 GB


Hard Disk or more

Software Requirements: 32/64 bit Linux(Ubuntu/Fedora)Operating System, latest any


Python Tool Like Anaconda,Pycharm, Python IDLE, Python Atom,Eclipse.

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.

A string is a list of characters inorder.


A character is anything you can type on the keyboard in
onekeystroke, like a letter, a number, or abackslash.

Strings can have spaces: "hello world".

An empty string is a string that has 0

characters. Python strings are immutable

Page 17
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)

Python recognize as strings everything that is delimited by quotation


marks (" " or ' ').

StringManipulation

To manipulate strings, we can use some of Pythons built-in

methods. word = "HelloWorld"

>>> print
word Hello
World

Accessing

Use [ ] to access characters in a

string word = "Hello World"

letter=word[0]

>>>print
letter H

Length

word = "Hello World"

>>>len(word)
11

Finding

word = "Hello World"

>>>printword.count('l') # count how many times l is in


thestring 3

>>>printword.find("H") # find the word H in the


string 0

>>>printword.index("World") # find the letters World in the


string 6

Count

s = "Count,the number of spaces"

>>> print s.count('


') 8

Page 18
Trinity Academy of Engineering, Pune Programming and problem solving Lab(2019 Pattern)

Slicing

Use [ # : # ] to get set of letter

Keep in mind that python, as many other languages, starts to count from 0!!

word = "Hello World"

printword[0] #get one char of theword


printword[0:1] #get one char of the word (same as
above) printword[0:3] #get the first threechar
printword[:3] #get the first three
char printword[-3:] #get the last
threechar
printword[3:] #get all but the three first char
printword[:-3] #get all but the three
lastcharacter

word = "Hello World"

word[start:end] # items start through end-1


word[start:] # items start through the rest of
thelist
word[:end] # items from the beginning through
end-1 word[:] # a copy of the wholelist

Split Strings

word = "Hello World"

>>>word.split(' ') # Split on whitespace


['Hello', 'World']

Startswith / Endswith

word = "hello world"

>>>word.startswith("H
") True

>>>word.endswith("d
") True

>>>word.endswith("w
") False
Changing Upper and Lower Case Strings

string = "Hello World"

>>> print string.upper()

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

Reverse words in a given string

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 for Formatting Strings

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

You might also like