A Little Help Regarding Digital Root

This is a discussion on A Little Help Regarding Digital Root within the C Programming forums, part of the General Programming Boards category; I found an interesting problem regarding the 'digital root' of a number. In it you add the digits of a ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    36

    A Little Help Regarding Digital Root

    I found an interesting problem regarding the 'digital root' of a number. In it you add the digits of a number until you find a single digit. For example, digital root of 1032 would be 1+0+3+2=6 and digital root of 2354 would be 2+3+5+4=14=1+4=5.

    I made a recersive code about it but I can't arrive at a single digit. It gives you the sum of all the digits of the number but not the sum till a single digit. Here's the code:

    Code:
    #include<stdio.h>
    int rem=0;
    int digit_adder(int in)
    {
    	int newnum;
        rem = rem+(in%10);
        newnum=in/10;
    
        if(newnum==0)
        {
            return rem;
        }
        else
    		return digit_adder(newnum);
    }
    int main()
    {
        int no;
    	printf ("Enter the number: \n");
    	scanf ("%d", &no);
        int ans=0;
        printf("\n%d\n\n",(ans = digit_adder(no)));
    	return 0;
    }
    How can I change it into a code that gives me the root till a single digit?

    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It's be much easier if you just computed in%9
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Code:
    #include<stdio.h>
    int rem=0;
    int digit_adder(int in)
    {
      int newnum;
      rem = rem+(in%10);
      newnum=in/10;
    
      if(newnum==0)
        {
          if (rem > 9) { // Added
            newnum = rem; // Added
            rem = 0; // Added
            digit_adder(newnum); // Added
          } // Added
          return rem;
        }
      else
        return digit_adder(newnum);
    }
    int main()
    {
      int no;
      printf ("Enter the number: \n");
      scanf ("%d", &no);
      int ans=0;
      printf("\n%d\n\n",(ans = digit_adder(no)));
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer confusion
    By Blackroot in forum C++ Programming
    Replies: 11
    Last Post: 09-12-2007, 12:44 AM
  2. Really basic string operation
    By bobthebullet990 in forum C Programming
    Replies: 6
    Last Post: 11-28-2005, 04:18 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 01:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21