469,366 Members | 2,198 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Seperating numbers

2
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 1164
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
Dargen
2
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by TonyB | last post: by
6 posts views Thread by Jo K. | last post: by
reply views Thread by AnkitAsDeveloper [Ankit] | last post: by
5 posts views Thread by raj | last post: by
7 posts views Thread by newstips6706 | last post: by
24 posts views Thread by pereges | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.