**Problem Statement**: There is a JAR full of candies for sale at a mall counter. JAR has the capacity N, that is JAR can contain maximum N candies when JAR is full. At any point of time. JAR can have M number of Candies where M<=N. Candies are served to the customers. JAR is never remain empty as when last k candies are left. JAR if refilled with new candies in such a way that JAR get full.

Write a code to implement above scenario. Display JAR at counter with available number of candies. Input should be the number of candies one customer can order at point of time. Update the JAR after each purchase and display JAR at Counter.

Output should give number of Candies sold and updated number of Candies in JAR.

If Input is more than candies in JAR, return: “INVALID INPUT”

import java.util.Scanner; class CANDIES { public static void main(String[] args) { Scanner in=new Scanner(System.in); int N=10,K=5; int sold; sold=in.nextInt(); if(sold==0) { System.out.println("INVALID INPUT"); System.out.println("NUMBER OF CANDIES AVAILABLE:"+N); } else if(sold>N) { System.out.println("INVALID INPUT"); System.out.println("NUMBER OF CANDIES AVAILABLE:"+N); } else if((N-sold)<K) { System.out.println("INVALID INPUT"); System.out.println("NUMBER OF CANDIES AVAILABLE:"+N); } else { System.out.println("NUMBER OF CANDIES SOLD:"+sold); N-=sold; System.out.println("NUMBER OF CANDIES AVAILABLE:"+N); } } } /* OUTPUT 3 NUMBER OF CANDIES SOLD:3 NUMBER OF CANDIES AVAILABLE:7 */

Request:-If you found this post helpful then let me know by your comment and share it with your friend.

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other.

Thanks

## 0 Comments