a oS Pral yblem Solving & Python Programming Labo
F ratory Tay:
_ ANSWERS TO LAB EXERCISES»
nou
1, AIM: To write a program to calculate Electricity bill,
ne .
ALGORITHM
Step-1 Start
Step-2 Read Units consumed
Step-3 IF Units> 0 and Units < 100
Amount= Units*1.5
FixedCharge=25
Step-4 Else IF Units>100 and Units < =200
Amount=(100*1.5)+(Units-100)*2.5
FixedCharge=50
Step-5 Else IF Units>200 and Units < =300)
Amount=(100*1.5)+(200-100)*2.5 + (Units-200)*4
FixedCharge=50
Step-6 Else IF Units > 300
Amount=2500
FixedCharge=75
Step-7 Else Amount=0
Step-8 Total=Amount +FixedCharge
Step-9 Print Total
Step-10 Stop
FLOWCHART
Yes
Vameunetins ss | /Armuete(100 1 3}¢ feces
Feechagenes |e 22, al feescceees ee
=A
| PROGRAM
et
i = ]
| 3 | rogram for catculating electricity bill in Python |
| 3 units=int(input("Number of units consumed: "))
| 4 if(units> 0 and units < =100): |
ls Amount =units*1.5 |
Ie | ____ FixedCharge=25.00 |
| 7 | elif(units> 100 and units < =200):
Is | Amount =(100*1.5)+ (units-100)*2.5
9 FixedCharge=50.00
elif(units > 200 and units < =300):
|
H Amount =(100*1.5)+ (200-100)*2.5 + (units-200)"4 |
2] ____ FixedCharge=50.00
B_«eliflanits> 300):
i“ Amount=2500;#fixed rate
is FixedCharge =75.00 |
6 | else: |
v Amount=0; 1
18 Total= Amount+FixedCharge
print("\nElecticity Bill:%.2f" %Total);
|
\ OUTPUT
Number of units consumed: 98
Electicity Bill : 172.00
>>> |
Number of units consumed: 125
| Electicity Bill : 262.50
>>> |
Number of units consumed: 245 |
Electicity Bill : 630.00 |
| >>> |
| Number of units consumed: 350 |
| Electicity Bil! : 2575.00 |
>>> IAIM: To write a program for retail shop billing
THM
Step-1 Start
Step-2 Read 3 items to pl,p2,p3 and quantities to q1,q2,q3
Step-3 tot! =pl*ql
Step-4 to12=p2*q2
Step-5 tot} =p3*q3
Step-6 Amount=tot! +tot2+tot3
Step-7 Print Amount
Step-8 Stop
FLOWCHART_
To _
[Rend ot g2o3 ]
Z
(/ Pend o1.a2 3 /
tott=pixat
totd=p2xq2
tof9=p3xq9
ee
Amount=tott+tot2etotd
ma
[rant rom J
7
GRAM
ASimple program for grossory bill of 3 items
input("Name of the Productl:")
input("Quantuty:"))
iput("Name of the Product2:")
Minput("Quantuty:"))
put("Name of the Product3:")
(input("Quantuty:"))
tot =q1*55
tol =42*30
tot3 =4q3"25
Amount = totl +tot2+tot3
print("Product1:",p1,"Quantity!:", q1,"Totall:",totl)
| print("Product!:",p2,"Quantity!:", q2,"Totall:",tot2)
1 print("Product!:",p3, "Quantity 1:", q3,"Totall:",tot3)
| print("Total Amount:", Amount)
Roe tee ew amanOUTPUT ' |
Name of the Productl:Rice
Quantuty:10
Name of the Product2:Onion
Quantuty:5 |
Name of the Product3:TOmoto |
Quantuty:2 |
Productl: Rice Quantity1: 10 Totall: 550 |
Productl: Onion Quantity1: 5 Totall: 150
Productl: TOmoto Quantity!: 2 Totall: 50 |
Total Amount: 750
3. AIM: TO write a program to print sine series.
Step-1 Start
Step-2 Read x and n
Step-3 Convert x values into radian using formula x<— x*3,1412/180
Step-4 tex, Sume x
Step-5 Setup for loop from i< 1 until (i>>
Enter a bike name : Yamaha MTI5
Yamaha MT1S ~ 100 kg
>>>
Enter a bike name : TVS Apache RTR
TVS Apache RTR ~ 250 kg
>>>
Enter a bike name : Honda Bike
Honda Bike ~ 400 kg
>>>
Enter a bike name : TVS Sport
TVS Sport ~ 180 kg
>>>AIM: To write a program to find weight ofa steel bar.
ALGORITHM
Step-1 Start
Step-2 Rad Diameter of steel
Step-3 Read length of bar in meter
Step-4 Weight=Diameter” x Length/162
Step-5 Print Weight
Step-6 Stop
'FLOWCHART
bar in mm
‘Weight=Diameter? x Lenath/162
a
PROGRAM _
ee
#Program to print Weight of a Steel Bar
diameter=int(input("What is the Diameter of Steel Bar(in mm):"))
| length =int(input("What is the Lenght of Steel Bar(in meters):"))
| weigth=(diameter*diameter)*length/162
| print("Weight of",diameter,"mm diameter and",length, "meter length
} steel is:", weigth,"Kgs")
OUTPUT
What is the Diameter of Steel Bar(in mm):12
What is the Lenght of Steel Bar(in meters):60 i
Weight of 12 mm diameter and 60 meter length steel is: 53.333333333333336 KgsAIM: To write a program to compute Electrical Current in Three
Phase AC Circuit.
Step-1 Start |
Step-2 Read resistance (in ohms), inductive resistance (in ohms)
Step-3 Read load voltage (in V), farad (in Hz)
Step-4 V_Ph = V_Lisqrt(3)
Step-S_ Z Ph = sqrt( (R¥*2) + (X_L**2) )
Step-6 Ph = V_PH/Z Ph
Step-7 TL = 1 Ph
Step-8 RPh =R
Step-9 phi= acos(R_Ph/Z_Ph)
Step-10 pf= cos(phi)
Step-11 P = sqrt(3)*V_L*I_L*cos(phi)
Step-12 Print "The line current in A is",round(I_L,2)
Step-13 Print "The Power factor is:", pf »"degrees lag."
Step-14 Print "The power supplied in W is",P
Step-15* Stop
LFLOWCHART.
Read resistance (in ohms), inductive
Fesistance (in ohms), load voltage (in V),
farad (in H2)
V_Ph =V Lisqrt{3}
Z_Ph=sqrt{ (R™2) + (X_L"2) )
LPh=V_PhZ Ph
LL=LPh
RPh=R
phi= acos(R_PhiZ_Ph)
p= cos(phi)
P= sqrt(3)"V_L"L_L*cos(phi)
Print “The line current inA is“round{t_L,2)
Print “The power factor is:", pf,"degrees lag.”
Print “The power supplied in Wis" P“PROGRAM
‘alculate the line current, power factor and power supplied
from math import cos,acos,sqrt :
R = int(input("Enter a resistance (in ohms):")); # in ohm
int(input("Enter a inductive resistance (in ohms):")); # in ohm
int(input("Enter a load voltage (in V):")); # in V
Sf = intlinput("Enter the farad (in Hz):")); # in Hz
#Calculations
V_Ph = V_L/sqrt(3); # in V
Z_Ph = sqrt( (R**2) + (X_L**2) ); # in ohm
IPh = V_Ph/Z_Ph; # in A
[L = 1 Ph; # in A
print("The line current in A is",round(I_L,2))
RPh = R; # in ohm
phi= acos(R_Ph/Z_Ph); i
# Power factor
Pf= cos(phi); # in radians
rint("The power factor is : ",pf, "degrees lag.")
P = sgrt(3)*V_L*I_L*cos(phi); # in W
print("The power supplied in W is",P)
OUTPUT
Enter a resistance (in ohms): 10
Enter a inductive resistance (in ohms) : 7
Enter a load voltage (in V) : 200
Enter the farad (in Hz) : 25
The line current in A is 9.46
The power factor is : 0.8192319205190405 degrees lag.
The power supplied in W is 2684.5637583892617
>>>
Enter a resistance (in ohms): 30
Enter a inductive resistance (in ohms) : 45
Enter a load voltage (in V) : 1200
Enter the farad (in Hz) : 150
The line current in A is 12.81
The power factor is : 0.5547001962252291 degrees lag.
The power supplied in W is 14769.230769230773
>>>AIM: TO write a program to exchange the values of two variables.
#Using another variable
Read values for a, b
Print a, b
temp=a
a=b
b=temp
Print a, b
#Without using another variable
a=at+b
b=a-b
a=a-b
Print a, b
Step-14> Stop
FLOWCHARTkone
|” PROGRAM -
# Program to swap 2 variabes i
a = int(inpul('Enter value of « '))
int(input('Enter value of 6: ')
b
# using another variable
temp =a
a=b
b = temp .
# without using temporary variable
fa =a+b
th =a-b
tu =a-b
print(f'The value of a after swapping: {a}')
print(f'The value of b after swapping: {b}')
OUTPUT
Enter value of a: 30
Enter value of b: 43
The value of a after swapping: 43
The value of b after swapping: 30
>>>
Enter value of a: 87
Enter value of b: 65
The value of a after swapping: 65
The value of b after swapping: 87
>>>
Enter value of a: 123
Enter value of b: 789
The value of a after swapping: 789
The value of b after swapping: 12.
nine ‘apping: 123
Enter value of a: 12345
Enter value of b: 67890
The value of a after swappi;
Swapping: 67890
The value of b after Swapping: 123458. AIM: To write a program to circulate the values of ‘n’ variables.
ALGORITHM
Step-1 Start
Step-2_ Read the upper limit n
Step-3 Read n values and append to list
Step-4 Print list
Step-5 FOR i in RANGE(@,n, 1)
Step-6 Pop out current element
Step-7 Add element to list
Step-8 Print list
Step-9 Stop
_FLOWCHART |#Program for circ
¢ the values ofn va. ables
n = int(input("Enter number of values : "))
list! = 0
for iin range(0,n, 1): | ,
x = int(input("Enter integer : ))
list1.append(x) /
print("Circulating the elements of list ", listl)
for i in range(0,n,1):
x = listl.pop(0)
list1.append(x)
OUTPUT
Enter number of values : 3
Enter integer : 1
Enter integer : 2
Enter integer : 3
Circulating the elements of list [1, 2, 3]
2, 3,
B, 1, 2
OH, 2, 3]
Enter number of values : 5
Enter integer :
Enter integer :
Enter integer :
Enter integer :
Enter integer :
Circulating the elements of li
B34sn of list [1, 2, 3, 4, 5]
2B, 4, 5, 1, 2]
14, 5, 1, 2, 3
13, 1, 2, 3, 4]
O, 2, 3, 4, 5]
>>>
WAWKNAIM: write a program to find distance between two .
ara ou
find di ‘¢ between two points.
Step-L Start
Step-2 Read coordinates xt yl.a2,y2
Step-3 distance =((x2-x1)*"2 + (y2-y1)**2)"0.5
Step-4+ Print distance . .
Step-5 Stop
FLOWCHART
5 __
| distnce=(ixzaty2nyesny-2y08 |
Printaistance ff
ey
“PROGRAM
# Python Program to Calculate Distance
# Read co-ordinates
x1 = float(input('Enter x1: )
yl= float(input('Enter yl: '))
<2 = float(input('Enter x2: '))
‘float(input('Enter y2: '))
# i ‘ulculating distance
= ( (x2xI)**2 + (y2-yI)"2 ) * 0.5
sani Disa = %f’ %(d))
OUTPUT
Enter x1: 5
Enter y
Enter x2: 6
Enter y2: 7
Distance = 3.16227810.
factorial of the
AIM: To write a program (0 find the tiven num
ber,
ALGORITHM
Step-1 Start
Step-2 Enter a_ number . . .
Step-3 Set a loop to find the factorial of the given number
using the formula
fact =fact*i
Step-4— Print the factorial of the given number
Step-5 Stop
“FLOWCHART
PROGRAM
Sact=1 fanaa
2 num = (int(input("Enter the number:",
7 for iin range(1,num +1): Tee
: Sact=fact*i
print("The factorial of", num, "iss" fact)
OUTPUT
Enter the number:s
The factorial of § is; 12911. AIM: To write a program to generate the Fibonacci series.
ALGORITHM
Step-1 Start
Step-2 Read and Check whether the number is zero or not
If zero print Zero value. If not zero go further
Step-3 Set a loop upto the given number
Step-4 fib=fib-++a; a=b; b=c
Step-5 Every increment in the loop prints the value of fib
Step-6 After the execution of the loop stops the program
[FLOWCHART
num rating "Enter the Upper Limit:")))
print( "FIBBONACI SERIES")
| if(num == 0):
I print("0")
{| else:
for i in range(I, num+1):
Sib=fibta
a=b
10 b=fib
| print(fib,end="\")
OUTPUT
Enter the Upper Limit:10
FIBBONACI SERIES
0 1 1 2 3 5 8 13 4—- --- a
12. AIM: To write a program to find sum of Digs, and qa SS
the
given number is Palindrome or not.
ALGORITHM
Step-1 Start
Step-2 Enter the number
Step-3 Set a loop upto the number is not equal to zero
Step-4 Rem <— num % 10
Step-5 Sum < Sum +rem
Step-6 Rnum < rum *10+rem |
Step-7 Num < num/10
Step-8 After the end of the loop print the sum and reverse numg,
of the digit t
Step-9 Find whether the reverse number is equal to the given
number or not. If equal the number is palindrome
Step-10 If not equal the given number is not palindrome
FLOWCHART
| rem= nuntit0
| sum= sum rom
num = roam 10+ fom
‘sum num! 40
J int eurberie Jf / Pant ures 6
{_Palindrome not aPalindrone,
é ‘Stop )
int(input("Enier the number..."))y
m= O;rnum=0
3 while(aum!sum=sum-+rem
6 | num*10)-+ rem
7 num =int(numV/10
is | print("The sum of the digits of",a,"is:",sum)
9 print("The reverse number of the",a,"is:",rnum)
10 | ifa==mum):
iu | print("The given number is a palindrome")
|) 12 | else:
B | print("The given number is not a palindrome")
OUTPUT
Enter the number...123
| The sum of the digits of 123 is: 6
1 The reverse number of the 123 is: 321
I The given number is not a palindrome
| Enter the number...121
] The sum of the digits of 121 is: 4
(| The reverse number of the 121 is: 121
iL The given number is a palindrome
13. AIM: To write a program to print the pascal triangle
ALGORITHM _
Step-1 Start
Step-2 Read no of lines n
Step-3 Declare temp tuple af ]
Step-4 FORi = lton
Step-4.1 Append 1 to to afi]
Step-4.2 FOR j = 1 toi
Step-4.3 Append (afi-1][j-1]+afi-1][j]) to afi]
Step-4.4 ENDFOR
Step-5 IF n!=O then Append 1 to afi]
Step-6 FNDFOR
Step-7 FOR i= 1lton
Step-7.1 Print (n-1)
Step-7.2 FOR j = 0 to i+1
Step-7.3 Print('{0:6}' .format(alil[j))
Step-7.4 ENDFOR
Step-8 ENDFOR
Step-9 Stop———
“FLOWCHART
it(input( "Enter number of rows: "))
a=[]
Sor i in range(n):
a.append({])
afi).append(1)
for j in range(1,i): ;
afi.append(afi-1]fj-1] + afi-UGi)
iff! =0):
4fi-append(1)
Sor i in range(n):
prin" "*(n-i)end="""\sep=" ")
Sor j in range(0,i+1):
print('{0:6}'.format(afi}{j]),end="
print()
OUTPUT
Enter number of rows: 5
1
1 1
I 2 1
1 3 3
1 4 6 :
ms sep="")AIM
Program to find whether the number is
[ALGORITHM
Step-I
Step-2
Step-3
Step-4
Step-5
Step-6
Step-7
Start
ic 2, R num
Is i<=num-1 else goto step-9
Is num%i=0
Step-4.1 print not prime
Step-4.2 goto step-9
icitl
goto step-4
Is i=num print prime
Stop
T
num =(int(input("Enter the number...:")))
while(i< =num-1):
if(num%i = =0):
print("The given number is not a prime number")
i+]
if(i==num):
print("The given number is a prime")
OUTPUT
Enter the number.
The given number is a prime
Enter the number.
The given number is not a prime numberhe largest of the three AuMbers,
Step-1 Start
Step-2 Read X.y.2
. oy and x>z then big = X
step-5 ELSE y>z then big = y
Step-6 print big
Step-7 Stop
[PROGRAM !
x=(int(input("Enter First Number:")))
y=(int(input("Enter Second Number:")))
2=(int(input("Enter Third Number:")))
big =
if (x oi yand x > y:
big =
elif(y > x:
big =
Print ("Biggest oe "
OUTPUT
Enter First Number:2
Enter Second Number:4
Enter Third Number:3
Biggest of 2,4, 3 is: 4
| 0 co Ok16. AIM: To write a program to print the ascending and descending
order of the given array. !
| ALGORITHM
Step-1 Start
Step-2 Enter the size of the array
Step-3 Enter the elements of the array
Step-4 Set a loop up to the array size minus one
Step-5 Set a inner loop up to the size of the array
Step-6 Check whether the next array element is greater than or not
Step-7 If greater than exchange their position
Step-8 If not greater than then go to the loop
Step-9 After the execution of the inner loop the outer loop is executed
Step-10 Print the ascending order of the given array
Step-11 Print the descending order of the given array
Step-12 Stop
| FLOWCHARTfrom array import *
numearay(i' De,
print("Enter Upper Limit...")
no=ine(input())
print( "Enter the num
fori in range(0,no):
a=int(input())
num.append(a)
for iin range(0,no):
for jin range(i+1, no):
if(numfi]< num{[j]):
a=num{i]
num{i]=num[j]
num[j]=a
print("The ascending order of the given numbers")
for iin range(0,no):
print(numfi],end="")
print("\nThe descending number of the given numbers")
for j in range(no-1,-1,-1):
print(num[j],end=" ")
bers")
OUTPUT
Enter Upper Limit...
5
Enter the numbers
10
#0
50
0
20
The ascending ordi .
50 4090 200 he given numbers
The descendin, |
10 20 30 40 0 number of the given numbers