Check out example codes for "To add all the digits of a number till you get a single digit.". It will help you in understanding the concepts better.

Code Example 1

#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; 
}

Learn ReactJs, React Native from akashmittal.com