Thread: How do I add the digits of a number ?

  1. #16
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by manav View Post
    ajayd, u want complete code? PM me! $0.5 per line
    Now this is spam! I don't care if you have started a "homework service" but please don't post your add here.
    Last edited by abh!shek; 02-06-2008 at 06:00 AM.

  2. #17
    Banned
    Join Date
    Nov 2007
    Posts
    678
    oops! i almost became an illegal intruder for your thread! sorry!

    i should stay on topic! right?

    Code:
    char buff[16];
    int n = 12345;
    int s = 0;
    int i = 0;
    sprintf(buff, "%d", n);
    while (buff[i]) s += buff[i++] - '0';
    printf("Number: %d\nSum of digits: %d\n", n, s);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 09:57 PM
  2. To get the sum of digits of a five digit number
    By hum_tum2005007 in forum C Programming
    Replies: 8
    Last Post: 06-15-2008, 04:39 PM
  3. how to add 2 digits?
    By nefsan in forum C Programming
    Replies: 16
    Last Post: 03-28-2008, 02:41 PM
  4. Replies: 7
    Last Post: 05-26-2003, 05:44 PM
  5. Finding digits in a number
    By MadStrum! in forum C Programming
    Replies: 5
    Last Post: 09-05-2002, 03:05 AM