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 <iostream>
using namespace std;
int main()
{
int leadingDigits, middleDigits, lastDigits;
int tempValue, original;
cout<<"Please enter 7- to 9-digit number.\n";
cin>>original;
tempValue = original / 1000;
lastDigits = original % 1000;
if you do it this way you will need to handle 1, 2 and 3 digit cases.
if 123456 %1000 is 456, that is a 3 digit case.
but 123056 % 1000 gives only 56: you need to do something.
and 123001 % 1000 gives 1, do something again!
a crude way is if statements, if result < 10 do something else if result < 100 do something else default ...
> I have a problem when I input number 10000000, output 10,0,0
You need to output 0 as 000.
In fact, anything <100 not in the leading position needs to be padded with zeros.
The easiest way to generate the groups is to do it from right to left - using % and / operators.
You can move the code from the function into main, since you are not allowed to use functions.
#include <iostream>
#include <exception>
void pretty_print(int num)
{
if(num < 1'000'000 || num > 999'999'999)
throw std::exception("Invalid in put in function pretty_print");
int last_group = num % 1000;
num /= 1000;
int middle_group = num % 1000;
num /= 1000;
std::cout << num << ',' << middle_group << ',' << last_group << '\n';
}
int main()
{
pretty_print(1234567);
pretty_print(12345678);
pretty_print(12345679);
}