Skip to content

Commit

Permalink
Adding code for finding sum of digits of a number using recursion in C
Browse files Browse the repository at this point in the history
  • Loading branch information
riyapannu7 committed Oct 15, 2017
1 parent 98eaf53 commit 14a259a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions c/Recursion/sum_of_digits_with_recursion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//loop
int sumOfDigits(int number)
{
int sum = 0;
while(number != 0)
{
sum += (number % 10);
number /= 10;
}
return sum;
}

//recursive
int recurSumOfDigits(int number)
{
if(number == 0)
return 0;
return ((number%10) + sumOfDigits(number/10));
}

int main()
{
int num;
printf("Enter number to be summed: ");
scanf("%d", &num);
printf("Digits summed: %d\n", sumOfDigits(num));
printf("Digits summed: %d", recurSumOfDigits(num));
}

1 comment on commit 14a259a

@riyapannu7
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolving issue #11

Please sign in to comment.