answersLogoWhite

0

#include <stdio.h>

int digit_sum(int);

int digit_sum_rec(int);

main()

{

int num;

printf("Enter 5 digit positive integer. : ");

scanf("%d",&num);

printf("The sum of the digits is: %d\n",digit_sum(num));

printf("The sum of the digits is (calculated recursively): %d\n",

digit_sum_rec(num));

}

int digit_sum(int n)

{

int t=0;

while(n) {

t+=n%10;

n/=10;

}

return t;

}

int digit_sum_rec(int n)

{

if(n==0) return 0;

return (n%10)+digit_sum_rec(n/10);

}

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

Can you provide a running example to illustrate the concept of recursion?

Recursion is a programming technique where a function calls itself to solve a problem. An example of recursion is the factorial function, which calculates the product of all positive integers up to a given number. For instance, the factorial of 5 (written as 5!) is calculated as 5 x 4 x 3 x 2 x 1 120. In this calculation, the factorial function calls itself with a smaller number until it reaches the base case of 1.


What is recursion in programing?

A recursive method (or function) is one that calls itself. Here is a popular example: The factorial function n! (read the exclamation mark as: factorial of n, or n factorial), for a positive integer, is the product of all numbers up to that number. For example, 4! = 1 x 2 x 3 x 4. In math, the factorial is sometimes defined as: 0! = 1 n! = n x (n-1)! (for n &gt; 0) You can write a function or method, using this definition. Here is the pseudocode: function factorial(n) if (n = 0) return 1 else return n * factorial(n - 1) Note that this is not very efficient, but there are many problems that are extremely complicated without recursion, but which can be solved elegantly with recursion (for example, doing something with all files in a folder, including all subfolders).


How to calculate the direction of a vector?

To calculate the direction of a vector, you can use trigonometry. Find the angle the vector makes with the positive x-axis using the arctangent function. This angle represents the direction of the vector in relation to the x-axis.


The discriminant of the function is?

positive


What is simulation recursion in C?

The factorial f(n) = n * (n-1) * (n-2) * .. 1. For example factorial 5 (written as 5!) = 5 x 4 x 3 x 2 x 1 = 120. The function below returns the factorial of the parameter n. int factorial( int n) { if (n==1) return 1 else return n* factorial( n-1) ; }


If a function is positive at a test number does the function has no zeros?

false!


How do you calculate the LCM of negative numbers?

The LCM is defined as being a positive integer. Ignore the negative signs. Calculate as if everything's positive.


Program for calculating factorial no using recursion in c?

factorial n is given by formula n! = n.(n-1)....1 int i; long x; x =1; for (i=n;i&gt;1;i--) x = x*i ; will calculate factorial. I have put x as long to avoid integer overflow. checks for n is positive etc. to be added.


Which the function's values become very positive or negative numbers?

The positive regions of a function are those intervals where the function is above the x-axis. It is where the y-values are positive (not zero). The negative regions of a function are those intervals where the function is below the x-axis. It is where the y-values are negative (not zero).


Linear function increasing?

A linear function is increasing if it has a positive slope. To find this easily, put the function into the form y=mx+b. If m is positive, the function is increasing. If m is negative, it is decreasing.


Give an example of a relation that is NOT a function and explain why it is not a function?

y&sup2; = x --&gt; y = &plusmn;&radic;x Because there are *two* square roots for any positive number (positive and negative) this will not be a function.


Is Sin a negative function or positive?

negative