Thread: Recursion to find sum of digits of n digit number ?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    41

    Recursion to find sum of digits of n digit number ?

    Hi,
    The See the below recursion.
    Code:
    #include<stdio.h>
    int rec(int);
    int main()
    {
        int n, b; 
        scanf("%d", &n);
        b = rec (n);
        printf("%d", b);    
        return 0;
    }
    int rec(int n)
    {
        
        if (n==0)
        return 0;
        else
        return (n%10+rec(n/10));
    }
    it works, but:
    Code:
    #include<stdio.h>
    void rec(int);
    int main()
    {
        int n;     
        scanf("%d", &n);
        rec (n);    
        return 0;
    }
    void rec(int n)
    {    
        if (n==0)
        rec(n)=0;
        else
        printf("%d", (n%10+rec(n/10)));
    }
    This is not working, is it possible to make it work?

    thanks.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Code:
    void rec(int);
    If the return type of a function is void it cannot return any value. These instructions are invalid:
    Quote Originally Posted by justine View Post
    Code:
    rec(n)=0;
    printf("%d", (n%10+rec(n/10)));
    Quote Originally Posted by justine View Post
    This is not working, is it possible to make it work?
    Yes. You can define a static variable which will hold the sum of digits.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    Code:
    #include<stdio.h>
    int rec(int);
    int main()
    {
        int n;    
        scanf("%d", &n);
        rec (n);   
        return 0;
    }
    int rec(int n)
    {   
        static int k;
        if (n==0)
        return 0;
        else
        {
            k=k+n%10;
            rec(n/10);
        }    
        printf("%d\n", k);
    }
    It prints many time the result.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Move printf here:
    Code:
    if (n == 0)
    {
        printf("%d\n", k);
        k = 0; // reset the sum for future call
    }
    By implementing this solution you didn't have to change function's return type to int.
    Last edited by DRK; 11-26-2012 at 04:26 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm confused with what you want to do. You have a working recursive program, but you don't want to use it. Do you want a non-recursive version of it (ie iterative version)?

    The first recursive version is as short and as clear as it can be. I guarantee you, you can't improve it. What's the goal here?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    @DRK I couldn't follow you, can you please give full program. @Adak I was just trying to print the result in function.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Quote Originally Posted by justine View Post
    @DRK I couldn't follow you, can you please give full program.
    Eh, since you've almost done it yourself...
    Code:
    #include<stdio.h>
    
    void rec(int n);
    
    int main()
    {
        int n;
        scanf("%d", &n);
        rec(n);
        return 0;
    }
    
    void rec(int n)
    {
        static int k;
        if (n == 0)
        {
            printf("%d\n", k);
            k = 0;
        }
        else
        {
            k += n % 10;
            rec(n / 10);
        }
    }

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    Thanks DRK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sum of digits of a number reducing to 1 digit.
    By amolkarale in forum C Programming
    Replies: 6
    Last Post: 09-10-2011, 05:55 PM
  2. Replies: 2
    Last Post: 10-31-2009, 06:49 PM
  3. five digit number with different digits
    By khdani in forum C Programming
    Replies: 11
    Last Post: 05-03-2009, 11:33 AM
  4. 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
  5. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM