jasonpogi1669

Sum of Digits using Recursion in C++

Oct 8th, 2021 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int Sum(int n) {
  6.     return (n % 10 + (n >= 10 ? Sum(n / 10) : 0));
  7. }
  8.  
  9. int main() {
  10.     int n;
  11.     cin >> n;
  12.     cout << Sum(n) << '\n';
  13.     return 0;
  14. }
  15.  
Add Comment
Please, Sign In to add comment