Skip to main content

Problem Statement

Write a C program that takes an integer as input and finds the sum of its digits using a while loop.

Solution

#include <stdio.h>

int main() {
    int n, sum = 0;
    scanf("%d", &n);
    while (n > 0) {
        sum += n % 10;
        n /= 10;
    }
    printf("Sum = %d\n", sum);
    return 0;
}

Input

1234

Output

Sum = 10