Splitting Up Numbers
-
Hello again, I have a lab assignment in my C++ class that asks me to do the following: -Create an application that prompts the user for two 3-digit numbers, then display the result in the following format: ex. 456 x 433 --------- 1368 13680 182400 --------- 197448 The problem I am having is the method I'm trying to use. I am using the carry over technique, and I need a way to split up the 3-digit numbers into single numbers, then multiply/carry/add accordingly Anything unclear let me know, Thanks!
-
Hello again, I have a lab assignment in my C++ class that asks me to do the following: -Create an application that prompts the user for two 3-digit numbers, then display the result in the following format: ex. 456 x 433 --------- 1368 13680 182400 --------- 197448 The problem I am having is the method I'm trying to use. I am using the carry over technique, and I need a way to split up the 3-digit numbers into single numbers, then multiply/carry/add accordingly Anything unclear let me know, Thanks!
You should think of an algorithm with the division ( / ) and mod ( % ) operators to split a number. For example: 513 % 10 = 3 -> Last digit 513 / 10 = 51 51 % 10 = 1 -> Second digit 51 / 10 = 5 5 % 10 = 5 -> First digit 5 / 10 = 0 -> END Think a bit.
-
You should think of an algorithm with the division ( / ) and mod ( % ) operators to split a number. For example: 513 % 10 = 3 -> Last digit 513 / 10 = 51 51 % 10 = 1 -> Second digit 51 / 10 = 5 5 % 10 = 5 -> First digit 5 / 10 = 0 -> END Think a bit.
Stewie Griffin wrote:
You should think of an algorithm with the division ( / ) and mod ( % ) operators to split a number.
Yes, typically. But in this case, the program is prompting the user for the numbers so it is already in string form. He can take the input string...
char[] n1 = "456"; /* or whatever the user enters */
...and "split the number" by accessing it character by character. Then covert each character to an int:
digit0 = n1[0] - '0'; /* or whatever conversion method you choose */
digit1 = n1[1] - '1';
/* etc. */ -
Stewie Griffin wrote:
You should think of an algorithm with the division ( / ) and mod ( % ) operators to split a number.
Yes, typically. But in this case, the program is prompting the user for the numbers so it is already in string form. He can take the input string...
char[] n1 = "456"; /* or whatever the user enters */
...and "split the number" by accessing it character by character. Then covert each character to an int:
digit0 = n1[0] - '0'; /* or whatever conversion method you choose */
digit1 = n1[1] - '1';
/* etc. */Robert.C.Cartaino wrote:
...the program is prompting the user for the numbers so it is already in string form.
So put them into a numeric variable (e.g.,
int
) instead. :rolleyes:"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
Stewie Griffin wrote:
You should think of an algorithm with the division ( / ) and mod ( % ) operators to split a number.
Yes, typically. But in this case, the program is prompting the user for the numbers so it is already in string form. He can take the input string...
char[] n1 = "456"; /* or whatever the user enters */
...and "split the number" by accessing it character by character. Then covert each character to an int:
digit0 = n1[0] - '0'; /* or whatever conversion method you choose */
digit1 = n1[1] - '1';
/* etc. */