Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. /* Make a "displayWithCommas(long n)" where n for now is a
  2. 4 to 6 digit number. Get it to just display the "thousands"
  3. part and the "other" part.
  4.  
  5. For example: displayWithCommas(12345);
  6.  
  7. Outputs:
  8. 12 is the thousands.
  9. 345 is the other.
  10. */
  11.  
  12. #include <iostream>
  13. using namespace std;
  14.  
  15. void displayWithCommas(long n) {
  16. if (n>=1000) {
  17. cout << n/1000 << ",";
  18. }
  19. int other = n%1000;
  20. //check to see how many digits left and handle appropriately
  21. if ((other >= 10) && (other <=99)) {
  22. //2 digit number
  23. cout << "0" << other << endl; //0xx
  24. }
  25. else if (other < 10){
  26. cout << "00" << other << endl; //00x
  27. }
  28. else {
  29. cout << other;
  30. }
  31. }
  32.  
  33. int main() {
  34. long n;
  35.  
  36. cout << "Enter a number: ";
  37. cin >> n;
  38. displayWithCommas(n);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement