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

Sum of Digits of a Five Digit Number

  • + 0 comments

    I know it is ugly but it is my logic :D

    include

    include

    include

    include

    int main() {

    int n;
    scanf("%d", &n);
    //Complete the code to calculate the sum of the five digits on n.
    int a = n / 10000; 
    int b = (n-a*10000) / 1000;
    int c = (n-(b*1000)-(a*10000)) / 100;
    int d = (n-(c*100)-(b*1000)-(a*10000)) / 10;
    int e = (n-(c*100)-(b*1000) - (d*10) - (a*10000) );
    
    
    int sum = a + b+c+d +e;
    
    
    printf("%d",sum);
    return 0;
    

    }