Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (anonymous):

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 the numbers 1234567, 987654321, 20300045, & 10000000. this is the program i have so far #include using namespace std; int main () { int first3, middle3, last3; int original, temp; cout<<"enter a 7-9 digit number"<>original; temp=original/1000; last3=original%1000; first3=temp/100; middle3=temp%1000; cout<<"the number with a comma is "<

OpenStudy (anonymous):

get a string, loop over from 1 - strlen(string)-1 and print each character with a comma..

OpenStudy (rsmith6559):

Since you're outputting it with commas, it has to be a string, it can no longer be an int. Think about the problem working it as a string.

OpenStudy (anonymous):

since it's seven to nine characters you can use integers too.You just have to extract their digits

OpenStudy (anonymous):

well this what my teacher showed me then he said that i have to add four more lines to the middle3 and last3; i had it once but i forget it

OpenStudy (anonymous):

please can someone help me with this it is already late

OpenStudy (anonymous):

#include <cstdio> #include <stack> using namespace std; stack<int> S; void print(long long a) { while(a>0) { S.push(a%10); a/=10; } printf("%d",S.top()); while(!S.empty()) { printf(",%d",S.top(); s.pop(); } } int main(void) { long long a; scanf("%lld",&a); print(a); return 0; } I didn't test it, i hope it works

OpenStudy (anonymous):

i am not supposed to use a while loop

OpenStudy (anonymous):

why?

OpenStudy (anonymous):

ah, every three digits :P i thought it would be every digit.. my code needs a small correction then.

OpenStudy (anonymous):

because while we were learning this program my teacher didnt teach us any of the looping statements

OpenStudy (anonymous):

fight the power :P

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!