Exemple #1
0
int sumDigits(int n){
    int rc=0;
    if(n > 0){
        rc=n%10 + sumDigits(n/10);
    }
    return rc;
}
Exemple #2
0
/**
 * Given a non-negative int n, return the sum of its digits recursively (no loops).
 *
 * @param n The number to sum the digits of
 * @return The sum of its digits
 *
 * @note Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6),
 *  while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
 *
 * Example:
 *  sumDigits(126) == 9
 *  sumDigits(49) == 13
 *  sumDigits(12) == 3
 */
int RecursionExercises::sumDigits(int n)
{
  if (n<=0)
    return 0;
  return n%10 + sumDigits((n/10));

}
Exemple #3
0
	// Given a non-negative int n, return the sum of its digits recursively (no loops).
	int sumDigits( int n )
	{
		if( n <= 0 )
			return 0;

		return ( n % 10 ) + sumDigits( n / 10 );
	}
Exemple #4
0
int sumDigits(int n)
{
	/*if(n!=0)
   {
	int m;
   m=n%10;
   
   
   n=n/10;
   m=(sumDigits(n)+m);
   return m;
   }
   else 
   { int sum=0;
    return sum;; // Replace this with your code
}
*/


   if(n==0) return 0;
   else
{
int m=n%10;
n=n/10;
 return m+sumDigits(n);


}


}
Exemple #5
0
/**
 * Given a non-negative int n, return the sum of its digits recursively (no loops).
 *
 * @param n The number to sum the digits of
 * @return The sum of its digits
 *
 * @note Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6),
 *  while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
 *
 * Example:
 *  sumDigits(126) == 9
 *  sumDigits(49) == 13
 *  sumDigits(12) == 3
 */
int RecursionExercises::sumDigits(int n)
{
    if (n==0) return 0;
	int temp = n;
	temp = n%10;
	temp = temp + sumDigits(n/10);
	return temp;
}
Exemple #6
0
/**
 * Given a non-negative int n, return the sum of its digits recursively (no loops).
 *
 * @param n The number to sum the digits of
 * @return The sum of its digits
 *
 * @note Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6),
 *  while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
 *
 * Example:
 *  sumDigits(126) == 9
 *  sumDigits(49) == 13
 *  sumDigits(12) == 3
 */
int RecursionExercises::sumDigits(int n)
{
    if(n < 10){
	return n;
    }
    
    return sumDigits(n/10) + n % 10;
}
int main(void)
{
  int n;
  printf("Enter number to sum up: ");
  scanf("%d", &n);
  printf("%d \n", sumDigits(n));
  getchar();
  return 0;
}
int main()
{
	int num = 3748;
	printf("Ingresa un numero: ");
	scanf("%d", &num);
	printf("El numero: %d tiene %d digitos.\n", num, countDigits(num));
	printf("El numero: %d sus numeros suman: %d\n", num, sumDigits(num));
	return 0;
}
Exemple #9
0
int sf(int n) {
    // first check if value is in lookup table
    // int lookup = getLookupValue(table_f, n);
    // if( lookup != -1) { 
    //     return lookup; }
    // else {
        int fn = f(n);
    //     AddToTable(table_f,n,fn);
        return sumDigits( fn );
    // }
}
 bool isHappy(int n) {
     if (n<0) return isHappy(-n);
     set<int> myset;
     while (n!=1) {
         if (myset.find(n)!=myset.end()) return false;
         else {
             myset.insert(n);
             n = sumDigits(n);
         }
     }
     return true;
 }
/**
 * Given a non-negative int n, return the sum of its digits recursively (no loops).
 *
 * @param n The number to sum the digits of
 * @return The sum of its digits
 *
 * @note Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6),
 *  while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
 *
 * Example:
 *  sumDigits(126) == 9
 *  sumDigits(49) == 13
 *  sumDigits(12) == 3
 */
int RecursionExercises::sumDigits(int n)
{
    if(n==0){

    	return 0; //base case!
    }
    else
    	 return sumDigits(n/10)+  n%10;
    	 //n%10 gives the right most digit and n/10 cuts if off so add the two and were good to go!

    
}
Exemple #12
0
int main()
{
    // get input from user and save it to variable
    printf("Enter some number\n");
    long long input = GetLongLong();
    
    unsigned long sum = sumDigits(input);
    
    // display result
    printf("Sum is %lu\n", sum);
    
    return 0;
}
int main(){
    uint limit = 30;
    vector<ullint> v;
    for(ullint i = 0; i < 70; i++){
        for(ullint j = 0; j < 30; j++){
            ullint a = pow(i, j);
            ullint s = sumDigits(intToString(a));
            if(s == i)
                v.push_back(a);
        }
    }
    removeDuplicates(v);
    cout << v[limit + 9] << endl;
    return 0;
}
Exemple #14
0
int main(int argc, const char * argv[])
{
    int sizeofarray = 2*numbertofactorial;
    int* product;
    product = new int[sizeofarray];
    
    //initialize product array to 0s
    for (int i = 0; i < sizeofarray; i++) {
        product[i] = 0;
    }
    
    product[0] = 1;
    for (int multiplier = 2; multiplier <= numbertofactorial; multiplier++) {
        
//        printf("%d! = ", multiplier-1);
//        printCurrentProduct(product, sizeofarray);
//        ^uncomment above to see each factorial in succession^
        
        //find the index of the zero before the first nonzero
        int firstzeroinarray = sizeofarray;
        while (product[firstzeroinarray-1] == 0) firstzeroinarray--;
        
        int carry = 0;
        for (int index = 0; index < firstzeroinarray; index++) {
            product[index] = product[index]*multiplier + carry;
            if (product[index]/10 != 0) {
                carry = product[index]/10;
                product[index] %= 10;
            }
            else carry = 0;
        }
        product[firstzeroinarray] = carry;
        product = fixCarryIfNecessary(product, firstzeroinarray);
    }
    printf("\n%d! = ", numbertofactorial);
    printCurrentProduct(product, sizeofarray);
    int sum = sumDigits(product, sizeofarray);
    printf("The sum of the digits of %d! is %d.\n", numbertofactorial, sum);
    return 0;
}
int sumDigits(int no)
{
  return no == 0 ? 0 : no%10 + sumDigits(no/10) ;
}
Exemple #16
0
/**
 * Given a non-negative int n, return the sum of its digits recursively (no loops).
 *
 * @param n The number to sum the digits of
 * @return The sum of its digits
 *
 * @note Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6),
 *  while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
 *
 * Example:
 *  sumDigits(126) == 9
 *  sumDigits(49) == 13
 *  sumDigits(12) == 3
 */
int RecursionExercises::sumDigits(int n)
{	if(n/10 == 0)
	return n%10;
    	else
	return (sumDigits(n/10)+n%10);
}
Exemple #17
0
int sg( int i ) {
    sumDigits ( g ( i ) );
}
Exemple #18
0
// sum of digits
int sumDigits(int x) {
	if (x < 10) return x;
	return sumDigits(x/10) + sumDigits(x%10);
}