Question

C++: Write a recursive function that does the following: Given a number that could be up...

C++: Write a recursive function that does the following:

Given a number that could be up to 10 digits long, place the commas in the appropriate places.

PLEASE PAY ATTENTION TO THE FOLLOWING:

- Do not use the static modifier. No global variables.

- The input has to be in number format.

For example the number 1087045 would be displayed as

1,087,045

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <iomanip>
using namespace std;

void print_number(int n) {
    if (n < 1000) {
        cout << n;
    } else {
        print_number(n/1000);
        cout << "," << setw(3) << setfill('0') << n%1000;
    }
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    print_number(n);
    cout << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++: Write a recursive function that does the following: Given a number that could be up...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT