dimanche 11 octobre 2020

Sum of Digits of a Five Digit Number in c?Could you spot the error?

BTW i know that's not the most efficent way to do it but if i wanted to do it like i did,what i did incorrectly?The task was:Given a five digit integer, print the sum of its digits. Constraints 10000<=n<=99999 Sample Input 10564 Sample Output 16 CODE:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{
int n,sum;
int remainder_array[4]={n%1000;n%100,n%10,n};
int digits_array[4];
    scanf("%d",&n);
    //Complete the code to calculate the sum of the five digits on n.
    if(10000<=n && n<=99999)
    {
    else if(remainder_array[0]=0)
    {digits_array[0]=(n-remainder_array[0])/1000; n=remainder_array[1]; }
     else if (remainder_array[1]!=0)
    {digits_array[1]=(n-remainder_array[1])/100; n=remainder_array[2]; }
    else if(reminder_array[2]!=0)
    {digits_array[2]=(n-remainder_array[2])/10; n=remainder_array[3]; }
     else if(reminder_array[3]!=0)
    {digits_array[3]=n-remainder_array[3];}
    else {printf("%d",n/1000);}
    sum=digits_array[0]+digits_array[1]+digits_array[2]+digits_array[3];
    printf("%d",sum);
    }
    return 0;
}
`

Aucun commentaire:

Enregistrer un commentaire