Search C Program

Sum of digit by Recursion

/* Sum of digit by recursion */

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

int main()
{
 int i;
 printf(" Type any value : ");
 scanf("%d",&i);
 printf("Sum of digit : %d",sod(i));
 return 0;
}

int sod(int n)
{
 if(n<1)
  return 0;
 return(n%10+sod(n/10));
}

No comments:

Post a Comment