Write a program that accepts a 7-9 digit integer and echoes the number with commas between every three digits from the right c++. Do not use arrays, for loops, functions, pointers, or while loops. This
problem was designed to highlight the difference
between integer and real division.
I have a problem when I input number 10000000, output 10,0,0. I want to 100,000,00 but I do not how to code. Can you help me? Thank you so much. And this is my code
#include
Hi, and welcome to QuestionCove! I'll preface my response by saying I am not very familiar with some of the more advanced syntax of C++, but I think I understand the problem you're trying to solve as well as your approach. For this particular input, 10000000 (ten-million), the output you would expect is 10,000,000, but you're actually getting 10,0,0. This is simply because, in the sections for the "lastDigits" and "middleDigits", you are basing the output on the modulo calculation (%), which, as you may know, returns the remainder of the division between the two operands. So, in this example, original % 1000; resolves to 10000000 % 1000, the remainder of which is 0. In other words, this is why the statements returns single digit 0, rather than 3-digit 0. This would also be a problem if the result were single-digit or two-digit integer, not just for 0. So, what is the solution to this issue? I would suggest writing some sort of check to see if the result is less than 3-digits. If it is less than 3-digits, that means you will have to add zeroes (to the left of the result) in order to make the result exactly 3 digits in length. For instance, in this case, original % 1000 produces single-digit 0, so you would add two zeros and save that result to your "lastDigits" variable. If, as another example, the input was 10000042, original % 1000 would give you the result 42 and you would have to add one zero in order to get the appropriate result. I think this approach should solve your issue. Please feel free to reach out for additional support; I do enjoy discussing these types of problems, and I hope we can get it working for you :) I think this approach should give you the result you are looking for
As an addendum, I'd like to say that I think you've made good progress towards the solution and it seems you've already grasped the important difference between integer and real division that the problem was trying to highlight (namely that integer addition simply truncates any decimal values from the result, while real division preserves these values). The only problem with your current code is that it does not handle leading zeroes well, since the modulo operation (%) does not preserve those. The good part is that you know each segment of the output should be; you cleverly split the problem into 3 sections and defined variables to handle each of those. We know, based on the problem design, that middleDigits and lastDigits should always be exactly 3 digits in length. We also know that, because of the modulo (%), the only time when these variables are less than 3 digits is because of missing zero(es). That is the reasoning behind the solution I suggested above; basically, whenever middleDigits and lastDigits are less than 3 in length, concatenate them to 0 so that they are exactly 3 in length. I should also mention that the method I proposed may require you to convert integer to String (and possibly back, depending on your output), but it seems like this is simple to do in C++ if you know how [one way is to use the to_string() method on the integer you want to convert]. I am not sure if String+Integer concatenation is allowed in C++, wanted to include this little note just in case. In fact, I am not entirely sure that the solution I proposed is the best way to solve the problem you are facing, but I think it is *a* working solution, and it could be worth a try. Again, if you would like to discuss further, I am very open to that :)
Thank you so much. I will try to add code and fix it ^^
Sounds good! I hope it works, and I'll try to be around if you need anything :)
Join our real-time social learning platform and learn together with your friends!