// return true if a must appear before b in the sorted sequence // see: http://en.cppreference.com/w/cpp/concept/Compare bool compare( int a, int b ) // our predicate for sort { const int sa = sum_digits(a) ; const int sb = sum_digits(b) ; if( sa == sb ) return lex_compare(a,b) ; else return sa < sb ; }
int main() { std::string a,b; a="723"; b="323"; bigint A(a); bigint B(b); bigint C(1); bigint D(1); std::cout << "A.size = " << A.size << std::endl; std::cout << "B.size = " << B.size << std::endl; A.print(); B.print(); C=add_bigint(A,B); C.print(); std::cout << "-------------------------------------" << std::endl; D=pow_bigint(2,1000); D.print(); std::cout << "sum of digits = " << sum_digits(D) << std::endl; return 0; }
int main(){ clock_t begin, end; double time_spent; begin = clock(); // Begin solution char *prod = malloc( sizeof(char) * ( n + 1 ) ); for (int i = 0; i < n; i++) prod[i] = '0'; prod[n-1] = '1'; for(int i = 2;i < 100; i++){ multiply_by(prod, i); } int result = sum_digits(prod); printf("%d\n",result); free(prod); // End solution end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; printf("Found result in %f time\n", time_spent); return 0; }
// Return sum of digits in integer n // Pre-cond: n >= 0 int sum_digits(int n) { if (n == 0) { /* code */ return 0; } return n%10 + sum_digits(n/10); }
main() { int n = 123, x; printf("n = %d\n", n); x = sum_digits(n); printf("Sum of digits = %d\n", x); }
int main(void) { int num; printf("Enter a non-negative integer: "); scanf("%d", &num); printf("Sum of its digits = %d\n", sum_digits(num)); return 0; }
int sum_digits(int n) { int x, z; x = n % 10; if (n > 0) { n = n / 10; x = x + sum_digits(n); } return x; }
int main(int argc, char *argv[]) { FILE *file = fopen(argv[1], "r"); char line[80]; while ( fgets(line, sizeof(line), file) != NULL ) printf("%d\n", sum_digits(line)); fclose(file); return 0; }
int main(){ long long n; scanf("%lld",&n); /*we go on calculating the sum of the digits of the resulting number until it becomes less than or equal to 9*/ while(n>9) n=sum_digits(n); if(n==9) printf("YES\n"); else printf("NO\n"); return 0; }
constexpr bool is_valid(const Point &point, size_t limit = LIMIT) { return (sum_digits(absolute(point.x)) + sum_digits(absolute(point.y))) <= limit; }
constexpr size_t largest_extent(size_t limit = LIMIT) { int size = 0; while (sum_digits(++size) <= limit) { } return size; }
constexpr bool is_valid(const Point &point, size_t limit = LIMIT) { return (sum_digits(absolute(point.x)) + sum_digits(absolute(point.y))) <= limit; } constexpr size_t largest_extent(size_t limit = LIMIT) { int size = 0; while (sum_digits(++size) <= limit) { } return size; } static_assert(absolute(0) == 0, "test abs of zero"); static_assert(absolute(12345) == 12345, "test abs of positive"); static_assert(absolute(-12345) == 12345, "test abs of negative"); static_assert(sum_digits(123456789) == 45, "test sum_digits 1-9"); static_assert(is_valid({-5, -7}, 19) == true, "test accessible point"); static_assert(is_valid({59, 79}, 19) == false, "test inaccessible point"); static_assert(largest_extent(19) == 299, "test table size"); int main() { size_t constexpr N = largest_extent(LIMIT); std::cout << "largest: " << N << std::endl; auto quadrant = std::make_unique<Table<N,N>>(); uint64_t count = 0; std::queue<Point> points; // breadth-first points.push({0, 0}); while (!points.empty()) {
bool operator() ( int a, int b ) const { return sum_digits(a) < sum_digits(b) ; }
static int sum_digits( std::pair<int,int> p ) { return sum_digits( p.first ) + sum_digits( p.second ) ; }
int sum_digits(long long n){ return n?(n%10)+sum_digits(n/10):0; }
int sum_digits( int n ) { if( n < 0 ) return sum_digits( -n ) ; else if( n < 10 ) return n ; else return n%10 + sum_digits( n/10 ) ; }
bool operator() ( std::pair<int,int> a, std::pair<int,int> b ) const { return sum_digits(a) < sum_digits(b) ; }
//calculate the single digit number for fortune cookie int cookie_number (int z){ while(z >= 10){ z = sum_digits(z); } return z; }