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;
}
#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;
}
Comments
Leave a comment