Last active
April 16, 2019 16:13
-
-
Save Ashaba/46aad92cd80dc570b5f1c4576398ab56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package challenge; | |
/** | |
* You have 10,000/= bob, and you need to buy exactly 100 animals: | |
* - cows @1000/- | |
* - pigs @200/- | |
* - chicken @50/- | |
* | |
* 1. You must have at least one of each animal | |
* 2. You must use up all Ksh. 10K | |
* 3. You must buy exactly 100 animals | |
* | |
* How many animals can be bought | |
*/ | |
public class Animals { | |
public static void ComputePurchase(int Totalamount) { | |
int[] AnimalPrices = new int[]{ 1000, 200, 50 }; | |
String[] Animals = new String[]{"Cows", "Pigs", "Chicken"}; | |
int[] noteCounter = new int[3]; | |
for (int i = 0; i < 3; i++) { | |
if (Totalamount >= AnimalPrices[i]) { | |
noteCounter[i] = Totalamount / AnimalPrices[i]; | |
Totalamount = Totalamount - noteCounter[i] * AnimalPrices[i]; | |
} | |
} | |
// Print the combination of Animals to be bought | |
System.out.println("Animals to Purchase ->"); | |
for (int i = 0; i < 3; i++) { | |
System.out.println(Animals[i] + " : " | |
+ noteCounter[i]); | |
} | |
} | |
public static void main(String[] args){ | |
int amount = 10000; | |
ComputePurchase(amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OutPut: