Question
Here is the code for num_sum.c: #include void num_sum(int n, int *num, int *sum); //declaration of function void num_sum(int n, int *num, int *sum) {
Here is the code for num_sum.c:
#include
void num_sum(int n, int *num, int *sum); //declaration of function
void num_sum(int n, int *num, int *sum) { while (n > 0) //repeating number n until it is 0 { *num += 1; //incrementing # of digits *sum += n%10; //calculating sum of individual digits n /= 10; } }
int main(void) { //variables int n; int sum = 0, num = 0; //prompt user for number input printf("Enter a positive integer: "); scanf("%d", &n); //calling num_sum function into main num_sum(n, &num, &sum); //output results printf("The number has %d digit(s), sum of which is %d ", num, sum); return 0; }
EXTRA CREDIT (10 points) In order to understand recursion, you must first understand recursion" - found somewhere on the Internet. Trying Googling "recursion", this search is one of fun Easter eggs of Google search like searching "do a barrel roll". Can you reimplement your function, num_sum ), such that it is a recursive function Name you file num.sum rec.e. Example executions: $ gc-Wallonum_sum_rec num_sum_rec. .um_sum_rec Enter a positive integer: 5 The number has 1 digit (s), sum of which is 5 . / num rec
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started