Answer to Question #231494 in C++ for Son The Quang Nguy

Question #231494

Write a program that accepts a 7-9 digit integer and echoes the number with commas between every three digits from the right. Test it with numbers 1234567, 987654321, 20300045, & 10000000. And I have code my teacher requires me, must add code, because code has a problem. When I input 10000000 --> 10,0,0 not is 10,000,000. Help me, please.

#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;

 // Add code

 leadingDigits = tempValue / 1000;

 middleDigits = tempValue % 1000;

 // Add code

 cout<<"The number with commas is "<<leadingDigits;

 cout<<","<<middleDigits<<","<<lastDigits<<endl;

 return 0;

}



1
Expert's answer
2021-08-31T23:51:17-0400
#include <iostream>


using namespace std;


int main()
{
    int original;
    cout<<"Type a 7-9 digit number\n";
    
    cin>>original;
    
    int a0 = original%10;
    int a1 = (original/10)%10;
    int a2 = (original/100)%10;
    int a3 = (original/1000)%10;
    int a4 = (original/10000)%10;
    int a5 = (original/100000)%10;
    int a6 = (original/1000000)%10;
    int a7 = (original/10000000)%10;
    int a8 = (original/100000000)%10;    
        
    cout << a8 << a7 << a6 << ",";
    cout << a5 << a4 << a3 << ",";
    cout << a2 << a1 << a0 << endl;


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS