Q22 How to add sum the digits of a given number in single statement?

Twisted question from interviewer, but not difficult.

for(; num > 0; sum += num%10, num /= 10);

printf(“Sum = %d\n”,sum);

or we can right like this

while((sum = sum+(num%10)) && (num = num/10) > 0);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.