Monday, September 12, 2011

sum of digits of given number using Recursion

//sum of digits of given number using Recursion
#include<stdio.h>
#include<conio.h>
main()
{
    int a,n;
    clrscr();
    printf("\n enter any number:");
    scanf("%d",&n);
    a=sumdig(n);
    printf("\n sum of digits of the number =%d",a);
    getch();
    }

    sumdig(int num)
    {
    static int sum;
    int a,b;
    a=num%10;
    b=(num-a)/10;
    sum=sum+a;
    if(b!=0)
        sumdig(b);
    else
        return(sum);
    }

No comments:

Post a Comment

Popular 5 Posts Last 7 days