Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath> /* ceil, log10 */
  3. using namespace std;
  4.  
  5. // function declarations go here
  6. int add_next_digit(int sum, int n);
  7. int swap_values(int sum, int n);
  8.  
  9. int main(){
  10.  
  11. int n;
  12. int sum = 0;
  13.  
  14. cout << " ";
  15. cin >> n;
  16.  
  17. while(n >= 10){
  18.  
  19. int len = ceil(log10(n + 1));
  20.  
  21. for(int i = 0; i < len; i++){
  22.  
  23. cout << add_next_digit(sum, n);
  24.  
  25. if(n > 0){
  26. cout << " + ";
  27. }
  28. }
  29.  
  30. cout << " = " << sum << endl;
  31.  
  32. swap_values(sum, n);
  33.  
  34. };
  35.  
  36. cout << "Single digit is: " << n << endl;
  37.  
  38. return 0;
  39. }
  40.  
  41. int add_next_digit(int sum, int n)
  42. {
  43. while ( n > 0 ) {
  44. sum += n % 10;
  45. n /= 10;
  46. }
  47.  
  48. int swap_values(int sum, int n)
  49. {
  50. while ( n > 0 ) {
  51. sum += n % 10;
  52. n /= 10;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement