Home

A program to find and print sum of individual digits of a given number

C Programming Language 034

The following code shows how to write a program to find and print sum of individual digits of a given number.

Program

#include <stdio.h>

void main()
{
    int n, l = 0, sum = 0;
    printf("Enter a number: ");
    scanf("%d", &n);

    printf("Sum of individual digits of %d is ", n);

    while (n > 0)
    {
        l = n % 10;
        sum = sum + l;
        n = n / 10;
    }

    printf("%d", sum);
    printf("\n");
}

Result

Enter a number: 121
Sum of individual digits of 121 is 4


Last Updated on

Next Post: GraphProtocol: TS2322 null assignment in Subgraph →

Comments