C program for Sum of Digit using Recursion

If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>
main()
{
 int sum_of_digit(int);
 int n;
 printf("Enter 5 digit number\n");
 scanf("%d",&n);
 printf("Sum of digit of %d is %d\n",n,sum_of_digit(n));
}
int sum_of_digit(int n)
{
 if(n>0)
 {
  return(n%10+sum_of_digit(n/10));
 }
 else
  return(0);
}

OUTPUT:

Enter 5 digit number
14578
Sum of digit of 14578 is 25

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.