Advertisement
BojidarDosev

insert a number abc, receive output a+b+c; ( recursion)

Nov 12th, 2023
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.21 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int sum(int x)
  5. {
  6.     if (x==0)
  7.     {
  8.         return 0;
  9.     }
  10.     else
  11.     {
  12.         return x%10 + sum(x/10);
  13.     }
  14. }
  15.  
  16. int main() {
  17.  
  18.     int x;
  19.     cin >> x;
  20.     cout << sum(x);
  21.    
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement