Java program Fill a bucket with water using a mug

Question:

There is a Bucket of water capacity of x litres, which is supposed to fill with a mug of y millilitres water capacity. Say for example x=100 and y=10 then, bucket will get full of minimum of 8 mugs (>8-% and less than 100%>.

Bucket filling is to be stopped once more than 80% of bucket capcity is filled.

The amount of water taken at a time in mug is not fixed as it can be any value less than or equal to y. Notify to stop once bucket is full that is more than 80% of capacity of bucket and count number of mugs poured into bucket.

Note that Bucket capacity will always be greater that Mug’s Capacity.

Example 1:

INPUT VALUES

100 //Enter Bucket capacity in litres as this

20 // Enter Mug capacity in litres as this

// Enter Amount of water in MUG one after other as below

20

20

20

20

Code:

Bucket_water.java

import java.util.Scanner;
public class Bucket_water {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("INPUT VALUES");
        int bucketCap = sc.nextInt();
        if(bucketCap>0) {
            int mugCap = sc.nextInt();
            if(mugCap>0){
                double fullbucket = bucketCap*0.8;
                int fill=0;
                System.out.println("Below");
                while (fill<fullbucket){
                    int a = sc.nextInt();
                    if(a<=mugCap){
                        fill +=a;
                    }
                    else {
                        System.out.println("Invalid");
                    }
                }

            }
            else {
                System.out.println("Invalid");
            }
        }
        else {
            System.out.println("Invalid");
        }
    }
}

Recommended:

2 thoughts on “Java program Fill a bucket with water using a mug”

Leave a Comment

Your email address will not be published. Required fields are marked *