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

Sum of Digits of a Five Digit Number

Sort by

recency

|

755 Discussions

|

  • + 0 comments
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main() {
    	
        int n;
        scanf("%d", &n);
        //Complete the code to calculate the sum of the five digits on n.
        
        int digits[5] = {};
        int sum = 0;
        float exp = 10000;
        
        for(int i = 0; i < 6; i++){
            digits[i] = n / exp;
            n = n - digits[i] * exp;
            exp /= 10;
        }
        
        for(int j = 0; j < 6; j++){
            sum += digits[j];
        }
        
        printf("%d", sum);
        
        return 0;
    }
    
  • + 0 comments

    int main() { int n, res=0; scanf("%d", &n); for(int i=0; i<5; i++) { res+=n%10; n/=10; } printf("%d\n", res); return 0; }

  • + 0 comments

    Nice little warm-up! A great way to brush up on basic input handling and digit manipulation. Dental clinic in saskatoon

  • + 0 comments

    int main() {

    int n,i,sum=0;
    scanf("%d", &n);
    char aray[6];
    sprintf(aray, "%d", n);
    for(i=0;i<5;i++)
    {
        switch(aray[i]){
            case '1':sum=sum+1;;break;
            case '2':sum=sum+2;break;
            case '3':sum=sum+3;break;
            case '4':sum=sum+4;break;
            case '5':sum=sum+5;break;
            case '6':sum=sum+6;break;
            case '7':sum=sum+7;break;
            case '8':sum=sum+8;break;
            case '9':sum=sum+9;break;
        }
    }
    printf("%d",sum);
    //Complete the code to calculate the sum of the five digits on n.
    return 0;
    

    }

  • + 0 comments
    #include <stdio.h>
    
    void main() {
            int n, q, result=0;
            scanf("%d", &n);
            for(int i=0; i<5; i++) {
                    result+=n%10;
                    n/=10;
            }
            printf("%d\n", result);
    }