Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath> /* ceil, log10 */
- using namespace std;
- // function declarations go here
- int add_next_digit(int sum, int n);
- int swap_values(int sum, int n);
- int main(){
- int n;
- int sum = 0;
- cout << " ";
- cin >> n;
- while(n >= 10){
- int len = ceil(log10(n + 1));
- for(int i = 0; i < len; i++){
- cout << add_next_digit(sum, n);
- if(n > 0){
- cout << " + ";
- }
- }
- cout << " = " << sum << endl;
- swap_values(sum, n);
- };
- cout << "Single digit is: " << n << endl;
- return 0;
- }
- int add_next_digit(int sum, int n)
- {
- while ( n > 0 ) {
- sum += n % 10;
- n /= 10;
- }
- int swap_values(int sum, int n)
- {
- while ( n > 0 ) {
- sum += n % 10;
- n /= 10;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement