Posted by: Sourav | August 1, 2014

Sum of digits using recursion in C


 

#include<stdio.h>
int sum(int);

int main()
{
int r=sum(12345);
printf(“%d”,r);

}

int sum(int i)
{
int sum1=0;
int remain;
//printf(“%d”,sum1);
if(i!=0)
{
remain=i%10;
sum1=remain+sum(i/10);
//printf(“%d”,sum1);
}
return sum1;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: