Friday, 15 April 2011

Recursion Function to find the Sum of digit of N -Digit Number


#include<stdio.h>
#include<conio.h>

int SumRecursion(int );
void main()
{
int a=0,k=0;
printf("Enter any digit Number");
scanf("%d",&a);
k=SumRecursion(a);
}

int SumRecursion(int num1)
{
int sum=num1;                    //Initialize sum to the current number i.e. num1
sum=num1/10; // Divide The number by 10
int Reminder=num1-sum*10;       //Find the reminder of the number when divided by 10
num1=sum;                        // To use a recursive function we have to keep on   changing the num1 value

if(sum!=0)
{
Reminder=Reminder+SumRecursion(num1);
}
else
return Reminder;
}

No comments:

Post a Comment