answersLogoWhite

0


Best Answer

#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

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: A 5 digit positive integer is entered through the keyboard how to write a function in C to calculate sum of digits of the 5 digits number by using recursion and without using recursion?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

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).


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) ; }


The discriminant of the function is?

positive


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.


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

false!


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


When is a function would be positive real function?

A function is positive on an interval, say, the interval from x=a to x=b, if algebraically, all the y-coordinate values are positive on this interval; and graphically, the entire curve or line lies above the x-axis.on this interval.


How do you calculate the gamma function?

"Find a smooth curve that connects the points (x, y) given by y = (x &acirc;&circ;&rsquo; 1)! at the positive integer values for x."[Gamma]n = (n-1)!