Sum of Digits of a Five Digit Number Discussions | C | HackerRank

Sum of Digits of a Five Digit Number

  • + 0 comments
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main() {
    	
        int n,res,a=1;
        int sum=0;
        scanf("%d", &n);
        //Complete the code to calculate the sum of the five digits on n.
        int remaining=n;
        res= n%10;
        sum=sum+res;
        while(a<=4){
            remaining=remaining/10;
                   
            res=remaining%10;
    
            sum=sum+res;
    
            a++;
        }
        printf("%d",sum);
        return 0;
    }