Monday 31 July 2017

PPS 1


Practice Problems 1 Fall 17

1) Design an algorithm and draw a flow chart to check the given number is Armstrong number of three digits or not. A number is said to be Armstrong number if the summation of cube of digits in a three digit number is equal to the number. Check for boundary conditions, if the value entered is outside boundary conditions then enter 'Invalid input'.

Code:
print('Hello')

Input:
A number 'n'

Processing:
if(len(str(n))==3 and n>=100 and n<=999):
while(m):
s=s+(m%10)**3
m=m//10
if(s==n):
print('Yes')
else:
print('No')
else:
print('Invalid')

Output:
Print Armstrong or Not armstrong

Pseudo Code:
START
1) Read the number 'n'
2) s=0, m=n
3) if(len(str(n))==3 and n>=100 and n<=999), continue
4) while(m), s=s+(m%10)**3 and m=m//10 => Finding the sum of the cubes of digits
5) if n==s, print('Armstrong') else print('Not armstrong')
6) Else print('Invalid input')
END

Flow Chart:



******************************************************************************************


2) Devise an algorithm and draw a flowchart to simulate the working of an AND gate. AND gate takes two bits as input and output a bit as shown in the following table. Check for validity of input and print ‘Invalid input’ when user gives out of boundary values.

Code:
print('Hello')

Input:
Read two bits 'x' and 'y'

Processing:
if(x==1 and y==1):
     print(1)
else:
     print(0)

Output:
Print 0 or 1

Pseudo Code:
START
1) Read two bits 'x' and 'y'
2) If x=0 or x=1 continue, else print 'Invalid input'
3) If x=0 or x=1 continue, else print 'Invalid input'
4) if x=1 and y=1, print 1
5) else, print 0
END


Flow Chart:




******************************************************************************************

3) A health drink company, gives a festival offer to its retail customers. The health drink is packed in 1 kg pack. For every 5, one kg pack purchase, one 1kg pack will be free. (i.e. buy 5 get 1 free). Given the number of health drink packets purchased, design an algorithm and draw a flowchart to determine the total number of packets that the customer will get.

Code:
print('Hello')

=>
packets=int(input())
if(packets>0 and packets<=500):
   free = packets//5
   total = packets + free
   print(total)
else:
   print('Invalid input')


Input:
Number of health drink packets purchased

Processing:
if(n>0 and n<=500):
   free = packets//5
   total = packets + free
   print(totaal)
else:
   print('Invalid input')

Output:
Print the Total number of packets that the customer will get.

Pseudo Code:
START
1) Read the number of packets purchased 'n'
2) if n>0 and n<=500 ,continue else print 'Enter valid number of packets'
3) free = packets//5
4) total = packets + free
5) print (total)
END

Flow Chart:



******************************************************************************************

4) Every day morning and evening milk is brought from ‘n’ farms to a milk booth for sales. Given the amount of milk from ‘n’ farms, write an algorithm to compute total quantity of milk in the booth. For example, if milk comes from 3 farms in quantities 2 litres 300ml, 3 litres 700ml and 4 litres 600ml then the total quantity of milk in booth is 10litre 600ml.


Code:
print('Hello')

=>
n=int(input())
ml=0
for i in range(n):
l=int(input())
m=int(input())
ml=ml+m+l*1000
litre=ml//1000
ml=ml%1000
print(str(litre)+'litre'+str(ml)+' '+'ml')


Input:
Number of farms 'n'
Amount of milk from each of the 'n' farms

Processing:
for i in range(n):
l=int(input())
m=int(input())
ml=ml+m+l*1000
litre=ml//1000
ml=ml%1000


print(str(litre)+'litre'+str(ml)+' '+'ml')

Output:
Print the total quantity of milk in booth

Pseudo Code:
START
1) Read the number of farms
2) let count = 0 and total = 0
3) while(count<n):
4) Read the amount of milk from each of the n farms, litres
5) count = count + 1
6) total = total + litres
END

Flow Chart:



******************************************************************************************

5) Arjun, amith and sharma are friends. Given their marks in Maths, design an algorithm and draw a flowchart to find minimum of their marks.

Code:
print('Hello')

Input:
Read marks of Arjun in maths
Read marks of Amit in maths
Read marks of Sharma in maths

Processing:
m1=int(input())
m2=int(input())
m3=int(input())
if(m1>=0 and m1<=100 and m2>=0 and m2<=100 and m3>=0 and m3<=100):
min=m1
if(m2<min):
min=m2
if(m3<min):
min=m3
print(min)
else:
print('Invalid input')


Output:
Print the name of student who scored minimum marks in maths
Print the minimum marks in maths

Pseudo Code:
START
1) Read marks of Arjun in maths
2) Read marks of Amit in maths
3) Read marks of Sharma in maths
4) if(m1>=0 and m1<=100 and m2>=0 and m2<=100 and m3>=0 and m3<=100), continue
5) let min=m1
6) if(m2<min), update min=m2
7) if(m3<min), update min=m3
8) Print min
END

Flow Chart:



******************************************************************************************
THANK YOU FOR VISITING...
******************************************************************************************

No comments:

Post a Comment