Pages

Wednesday 12 September 2012

UVA - 11332 - Summing Digits

#include <stdio.h>

long digit(long x){
    if(x<10)
        return x;
    long sum=0;
    long temp=x;
    while(temp>0){
        sum+=temp%10;
        temp/=10;
    }
    return digit(sum);
}

int main()
{  long x=0,rs;
    while(1){
         scanf("%ld",&x);
         if(x==0)
            break;
         rs=digit(x);
         printf("%ld\n",rs);
        }
    return 0;
}

No comments:

Post a Comment