Pages

Tuesday, 16 June 2015

Sum of digits using recursion

Sum of digits using recursion

Coding:

#include<stdio.h>
int count(long n)
{
if(n==0)
return 0;
else
return (n%10)+count(n/10);
}
int main()
{
long b;
printf("Please enter the number:");
scanf("%ld",&b);
printf("Sum is=%d",count(b));
return 0;
}

Output:
Please enter the number:1245
Sum is=12