Answers for "how to add digits of a number"

1

To add all the digits of a number till you get a single digit.

#include<bits/stdc++.h> 
using namespace std; 
int digSum(int n) 
{ 
	if (n == 0) 
	return 0; 
	return (n % 9 == 0) ? 9 : (n % 9); 
} 
int main() 
{ 
	int n = 9999; 
	cout<<digSum(n); 
	return 0; 
}
Posted by: Guest on August-17-2020
0

sum the digits of an integer

def getSum(n)
  n.digits.sum
end

print "Sum of digits = ", getSum(555);
Posted by: Guest on May-27-2021

Code answers related to "how to add digits of a number"

Python Answers by Framework

Browse Popular Code Answers by Language