Thursday, 19 December 2019

Sum of digits using recursion in C

#include<stdio.h>
#include<conio.h>
int sum(int);
int main()
{
int n,s;
printf("Enter the number: ");
scanf("%d",&n);
s=sum(n);
printf("The sum of digits is %d",s);
return 0;
}
int sum(int n)
{
if(n<10)
return n;
else
return (n%10+sum(n/10));
}

No comments:

Post a Comment

Convey your thoughts to authors.