Home
Questions
Search
Forum
Contact
Guest Book
Polls!
Got a Question?
 
PREV
Programs
NEXT
 
(79 / 301)
 
 



How can we sum the digits of a given number in single statement?




Try something like this


# include<stdio.h>

void main()
{
  int num=123456; 
  int sum=0;

  for(;num>0;sum+=num%10,num/=10);  // This is the "single line".

  printf("\nsum = [%d]\n", sum);
}


If there is a simpler way to do this, let me know!


PREV
COMMENTS                                  INDEX                                  PRINT
NEXT



Last updated: November 3, 2005

www.cracktheinterview.com - Your destination for the most common IT interview questions, answers, frequently asked interview questions (FAQ), C Programs, C Datastructures for technical interviews conducted by the top IT companies around the world!