-2

I am beginner in C programming and I want to improve this program by asking the user to enter an integer N and then the program make the sum of its digits and if the result of this sum is greater than a one digit we are remaking the sum of these digits.

example: N= 123456 -----> S= 1+2+3+4+5+6 ---------> S= 21 ------------> S=3

    #include <stdio.h>
main()
{
int T[50];
int N;
int I;
long S;
printf("D (max.50) : ");
scanf("%d", &N );
for (I=0; I<N; I++)
{
printf("E %d : ", I);
scanf("%d", &T);
}
printf("T :\n");
for (I=0; I<N; I++)
printf("%d ", T);
printf("\n");
for (S=0, I=0; I<N; I++)
S += T;
printf("Sum : %ld\n", S);
return 0;
}

Thanks in advance ;)

  • 1
    Start with formatting & indenting your code properly. Note: do not use all-uppecase for variabes. It si one of the few commonly accepted styles to use all-uppercase names for macros/constants only. And finally: C allows to use more than one letter for names. Appreciate it by using self-explanatory names. – too honest for this site Dec 13 '15 at 02:47
  • E.g 1234 = 1*10^3 + 2*10^2 + 3*10^1 + 4*10^0 => 1*999 + 1 + 2*99 + 2 + 3*9 + 3 + 4 => (1*999 + 2*99 + 3*9) + 1 + 2 + 3 + 4 So 1234 % 9 == (1 + 2 + 3 + 4) % 9 – BLUEPIXY Dec 13 '15 at 02:52

3 Answers3

1

There is no need to use an array for the digits. You should get into the habit of splitting the code into small easy to understand functions too.

#include <stdio.h>

// assume n >= 0
int sum_of_digits (int n)
{
    int sum = 0;
    while (n > 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}

// assume n >= 0
int digital_root_sum (int n)
{
    // As long as n has more than one digit, we replace it
    // with the sum of its digits.
    while (n >= 10) {
        n = sum_of_digits (n);
    }
    return n;
}

int main()
{
    int n;

    printf ("n   : ");
    scanf ("%d", &n);
    printf ("sum : %d\n", digital_root_sum (n));
    return 0;
}
  • I'd use `do ... while`, but ok. Your usage of `n` is allowed, but I find it a bit irritating. Maybe you just rename it, because that is actually the crosssum. But at least you found the spacebar on your keyboard ;-) – too honest for this site Dec 13 '15 at 03:03
  • Fair enough. `do while` really might not be a good idea. I'm just irritated with the update of `n`, as that is actually the (cross) sum, while your `sum` is just a temp. I tend to use self-explanatory names from outer to inner scope, i.e. use shorter names in inner scope, while the outer scope gets more "speaking" names. Just a preference, I did not vote on your answer (I actually think OP just should do research on his own and prividing ready code might not get him further in learning). Btw: do not use recursion; most times it is just of academic use and does more harm than good. ... – too honest for this site Dec 13 '15 at 03:17
  • ... although I do not agree about efficientcy. At least `static` functions (should be every not exported function) with tail-recursion are often optimised to a loop by a modern compiler. – too honest for this site Dec 13 '15 at 03:21
  • Ok, +1 for the effort :-) – too honest for this site Dec 13 '15 at 03:23
  • Thank you very much 'Antoine Mathys' for your quick and warm response, you are truly a master, excellent as program and it is a great pleasure to have a response someone like you. thanks again bro – b. mounaime Dec 13 '15 at 17:20
0
#include <stdio.h>

int sumOfDigit(int n){
    if(10 > n)
        return n;

    int r = n % 9;

    return r ? r : 9;
}

int main(void){
    int N, T[50];
    int i, sum = 0;

    printf("D (max.50) : ");
    scanf("%d", &N );
    for (i=0; i<N; i++){
        printf("E %d : ", i);
        scanf("%1d", &T[i]);//scanf("%d", &T[i]);
        sum += T[i];        //sum += sumOfDigit(T[i]);
    }
    printf("T :\n");
    for (i=0; i<N; i++)
        printf("%d", T[i]);
    printf("\n");

    printf("Sum : %d\n", sumOfDigit(sum));

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

Thank you everyone for your warm response, that's very kind of you. I'll try your answers and I put you posted. I apologize for my English and thank you again