-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding code for finding sum of digits of a number using recursion in C
- Loading branch information
1 parent
98eaf53
commit 14a259a
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
14a259a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolving issue #11