C Program to Calculate Sum of Digits

C Program to Calculate Sum of Digits

To get sum of each digits in C here we used relational operator for entering in loop and printf function for showing result .

Sum of Digits in C :



#include<stdio.h>  
 int main()    
{    
    int n,sum=0,modulus;    
        printf("Enter a number:");    
        scanf("%d",&n);    
    while(n!=0)    
        {    
            modulus=n%10;    
            sum=sum+modulus;    
            n=n/10;    
        }    
      printf("Sum is=%d",sum);    
return 0;  
}

Output :