Answers for "sum of digits to single digit"

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
-2

sum the digits of an integer

#include <stdio.h>
 
int getSum(unsigned long long n) {
    int sum = 0;
    for (; n; n /= 10)
    	sum += n % 10;
    return sum;
}

int main(){
	printf("Sum of digits = %d\n", getSum(555));
}
Posted by: Guest on May-27-2021

Code answers related to "sum of digits to single digit"

Python Answers by Framework

Browse Popular Code Answers by Language