C++, https://www.dropbox.com/s/b0m1jvnjg6i3dtr/Screenshot%202015-11-28%2018.24.11.png?dl=0
I think it'd be easy enough if you convert it into a string first. Then the amount of commas you should need is string length/3 and you can work backwards starting at the end of the string and counting in 3s and inserting a comma. for example //check if length <3 do nothing //otherwise add commas appropriately
but how do I manipulate the string in C++ @bibby
PrintWithCommas (long int x) {int g=x; for(int i=0, g>0,i++) {g=g%10;sumOfDigits=i;} if sumOfDigits%3==0 {int h= sumOfDigits; while h!=0 { cout<<x-(x* power(10,h))<<","; h=h-3; } if sumOfDigits%3==1 {int h= sumOfDigits+2; while h!=0 { cout<<x-(x* power(10,h))<<","; h=h-3; } if sumOfDigits%3==2 {int h= sumOfDigits+1; while h>0 { cout<<x-(x* power(10,h))<<","; h=h-3; } }
not necessary to manipulate the original string. you can do something like this ``` //count back 3 spaces from the end, insert a comma there, //keep going in groups of 3 until we've run out the length of the string int pos=str.length()-3; string new; while (pos> 0) { new.insert(pos, ","); pos-=3; } ``` I think you could also do it using a counter. so like count++, if count>3 insert. let me test this out
but i have little problem with my code, first one i always had " ," at the end of what ever i'm writing lol
I'm sure there's something you can do with iomanip but I'm not too sure what specifically this way works fine though http://puu.sh/lBSVj/59a89edba7.png
nice!
yes but this is not solved recursively
also the user is going to enter a long integer , not a string
oh, misread it, anyhow you should be able to convert a long to a string without issue I'm not here to do your assignment for you
this may not be the prettiest, but something like this: void print_with_comma(long int xx) { if (xx<1000) cout << xx; else { print_with_comma(xx/1000); cout << "," ; xx= xx-(xx/1000)*1000; if (xx<10) cout << "0"; if (xx<100) cout << "0"; cout << xx; } }
Join our real-time social learning platform and learn together with your friends!