Challenge: Count the Digits

Lab C Intermediate 20 points

Read a positive whole number and print how many digits it has, and the sum of its digits.

Input

One positive integer.

Output

Digits: D, Sum: S

Example

Input

4827

Output

Digits: 4, Sum: 21
Sample input available Sample output available
Editor
C

Hints

n % 10 is the last digit.

n / 10 removes the last digit.

Repeat while n > 0.

Solution explanation

Each round takes off the last digit with % 10, adds it, then removes it with / 10.

Show solution
#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    int digits = 0;
    int sum = 0;
    while (n > 0) {
        sum += n % 10;
        n /= 10;
        digits++;
    }
    printf("Digits: %d, Sum: %d\n", digits, sum);
    return 0;
}
Output
Answer
HOME LABS COMMUNITY DASHBOARD