C++:
#include <iostream>
int digitsum(int n) {
if(n==0) return 0;
return (n%10)+digitsum(n/10);
}
int main(int argc, char* argv[]) {
std::cout << "Enter a number to calculate: ";
int x; std::cin>>x;
if(x<0) std::cout << "Sum is: " << digitsum(x*(-1));
else std::cout << "Sum is: " << digitsum(x);
return 0;
}
Python:
def digitsum(n):
if(n==0): return 0
return int((n%10)+digitsum(n/10))
x = int(input("Enter a number to calculate: "))
if(x<0):
print("Sum is:",digitsum(x*(-1)))
else:
print("Sum is:",digitsum(x))