Wednesday, 15 March 2017

C Program to calculate the sum of the the digits of a number

C program to calculate the sum of the digits of a number is given below

Program:

#include <stdio.h>
int main()
{
    int x,sum=0;
    printf("\nEnter the element x: ");
    scanf("%d",&x);
    while(x!=0)
    {
        sum=sum+(x%10);
        x=x/10;
    }
    printf("\n\nsum of digits is : %d",sum);
    return 0;
}

OUTPUT:


EmoticonEmoticon