471,863 Members | 1,439 Online

# Seperating numbers

The assinment is i gotta prompt user to enter the number of five, ten twenty, fifty cent coins and one and two dollar coins they have then add em together.
The hard part is i need to store the number of cents as a integer and the number of dollars as an integer (not just one double)

So yeah
How can i seperate the number
123456 into the two numbers
1234 and 56

To display \$1234.56
or any other way you could think of doing it

Thanks for any help
Aug 15 '08 #1
4 1221
blazedaces
284 100+
Make a class with an int cent variable and an int dollar variable.

As for your specific question, if you are inputted 123456 and you want to get 1234 and 56 extracted from it you should do the following:

First realize that 123456 = 6 *10^0 + 5*10^1 + 4*10^2 + 3*10^3 + 2*10^4 + 1*10^5.

AND realize that when you divide an integer will truncate the remainder.

With this you can do the following:

store 123456 and make a second variable 123456

divide by 100, you'll get 1234 (cause it's an int it'll throw the rest away). Now you have the first part. Then multiply by 100 again and you'll get 123400, subtract the original from that, 123456-123400 = 56, you have your second part, the cents.

Good luck,
-blazed
Aug 15 '08 #2
Nepomuk
3,112 Expert 2GB
With this you can do the following:

store 123456 and make a second variable 123456

divide by 100, you'll get 1234 (cause it's an int it'll throw the rest away). Now you have the first part. Then multiply by 100 again and you'll get 123400, subtract the original from that, 123456-123400 = 56, you have your second part, the cents.

Good luck,
-blazed
Much easier:
Expand|Select|Wrap|Line Numbers
1. int total_cash = 123456;
2. int dollars = total_cash / 100;
3. int cents = total_cash % 100;
The % symbol is the so called modulo operator, which gives you the remainder of a division.
So, as 123456 / 100 = 1234 + 56 / 100 it will give you 56.

Greetings,
Nepomuk
Aug 15 '08 #3
Tankyou so much guys!
Great help :P
Aug 15 '08 #4
blazedaces
284 100+
Much easier:
Expand|Select|Wrap|Line Numbers
1. int total_cash = 123456;
2. int dollars = total_cash / 100;
3. int cents = total_cash % 100;
The % symbol is the so called modulo operator, which gives you the remainder of a division.
So, as 123456 / 100 = 1234 + 56 / 100 it will give you 56.

Greetings,
Nepomuk
Yes, much easier...

-blazed
Aug 15 '08 #5