Below program first accept the positive number from user and then find out the sum of digits. for example if user enter 234 then sum of digit is 2+3+4 = 9
//Program Name: Write a function for Sum Of digits
// for example if user enter 121 then sum of digit is 1+2+1 = 4
#include<stdio.h>
#include<conio.h>
int sum_d(int n)
{
if(n==0)
return(0);
else
return(n%10+sum_d(n/10));
}//end of function
void main()
{ int no,sum;
printf("\n\tEnter a number to calculate digit sum:\t ");
scanf("%d",&no);
sum=sum_d(no);
printf(" \n\n\tSum of digit is =\t %d ",sum);
}//end of main
Output
Enter a number to calculate digit sum: 543
Sum of digit is = 12
Enter a number to calculate digit sum: 2
Sum of digit is = 2