# Thread: Programming help

1. Suppose that you have a variable that holds a monetary value in cents. Write statements in Java that will compute the number of \$1000 , \$1 and 1 cent needed to compose the amount.

Attempt :

we take a random number ( 43,567 ) and assign it to int cents ?

\$1000 x 43567 = 43 times ( 40,000) ?

i am not sure how to go about it, any help will be appreciate .  2.

3. bump :/  4. It sounds like a knapsack problem. Try greedy algorithm.  5. You'll have to use utils.math.Integer library for such a big number !  6. I think you want to divide...

If your var holds the number of cents (pennies) then you would want something like this:

int OriginalValue = 43567;
int NumOfDollars = 43567 / 100; //should equate to 435
int NumOfCents = 43567 % 100; //Should give you the remainder, 67

So from that number of cents you have 435 \$1 and 67 Pennies.

Lets try a bigger number, say 443567

int OriginalValue = 443567;
int NumOfDollars = 443567 / 100; //should equate to 4435
int NumOfThousands = NumOfDollars / 1000; //Should equal 4
NumOfDollars = NumOfDollars - NumOfThousands; // Should Equal 435

int NumOfCents = 443567 % 100; //Should give you the remainder, 67

So you now have 4 \$1000, 435 \$1 and 67 Pennies.

Does that work for you?

TN  Bookmarks
 Posting Permissions
 You may not post new threads You may not post replies You may not post attachments You may not edit your posts   BB code is On Smilies are On [IMG] code is On [VIDEO] code is On HTML code is Off Trackbacks are Off Pingbacks are Off Refbacks are On Terms of Use Agreement