#include
main()
{
int n, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d",&n);
while(n != 0)
{
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}
printf("Sum of digits of entered number = %d\n",sum);
return 0;
}
Add digits using recursion
#include
#include
int add_digits(int);
int main() {
int n, result;
scanf("%d", &n);
result = add_digits(n);
printf("%d\n", result);
return 0;
}
int add_digits(int n) {
static int sum = 0;
if (n == 0)
{
return 0;
}
sum = n%10 + add_digits(n/10);
return sum;
}
No comments:
Post a Comment