Error in Repeated digital sum in C -
digital sum of 65536 7, because 6+5+5+3+6=25 , 2+5 = 7 want make program above task. didn't correct answer when first addition above 10
program:
#include <stdio.h> int main() { int n, sum = 0, temp; scanf("%d",&n); while(1) { temp = n%10; sum += temp; n = n/10; if(n==0) { if(sum>=10) { n = sum; } else { break; } } } printf("%d", sum); return 0; }
you can digital sum directly doing, if number non-zero then
value=number % 9
if value 0 consider sum of digit equal 9.
read link idea how works
Comments
Post a Comment