Practice Problems 2(Water in a dam)

Water in a dam



  1. A city has a dam of capacity ‘x’ litres, water comes to the dam from ‘n’ places.  Given the value of ‘n’ and the quantity of water (in litres and millilitres) that comes from ‘n’ places, write an algorithm and the corresponding Python code to determine the total amount of water in the dam. Assume that the total quantity of water in the dam, will be always less than the capacity of the dam. For example, if there are three places from which water comes to the dam and the water from place 1 is 2 litres 500 ml, water from second place is 3 litres 400 ml and water from third place is 1 litre 700 ml then the total quantity of water in dam will be 7 litres 600 ml.



Input:

Get number of places from where water comes and store in variable n

get number of litres from place - i(litre)

get number of mililitres from place - i (mili)

where i goes from 1 to n



Processing:

let total_litre=0

let total_mili = 0

total_litre = total_litre + litre

total_mili = total_mili + mili

if total_mili > 1000 then

total_litre = total_litre + int(total_mili/1000)

total_mili = total_mili%1000



Output:

Display The total litres and the total millilitre



Algorithm:

Step1. Get the number of places from the user and store it in variable n.

Step2. Initialize i as 1 and total_litre and total_mili both as 0

Step3. Repeat until i is less than or equal to n

Step3.1 Get the number of litres of water from place i and store it in variable litre

Step3.2 Get the number of mililitres of water from place i  and store it variable mili_litre

Step3.3 total_litre is equal to the sum of itself and litre

Step3.4 total_mili is equal to the sum itself and mili_litre

Step3.5 increment i by 1

Step4. if total_mili is greater than or equal to 1000

Step4.1 total_litre is the sum of itself and integer division(total_mili/1000)

Step4.2 total_mili is equal to the remainder obtained when total_mili is divided by 1000

Step5. display total litres and total mililitres in dam

Step6. End

Program:

total_litre,total_mili = 0,0
number = eval(input())
for i in range(1,number+1):
    litre = eval(input())
    mili_litre = eval(input())
    total_litre += litre
    total_mili += mili_litre
if total_mili >= 1000:
    total_litre += int(total_mili/1000)
    total_mili = total_mili%1000

print(total_litre)
print(total_mili)

Flowchart:

damdupl.png

To edit this flowchart click on this link

2 comments:

  1. Nice stuff but it would be better if you use indentation in processing

    ReplyDelete
    Replies
    1. Thank You for your feedback....have started implementing it in the following problem sets....Hope you would come back and refer to the solution....Keep sharing it...

      Delete

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...