Write a program for calculating value of an integer, C/C++ Programming

Write a Program for Calculating Value of an Integer?

For a clear understanding of recursive function we shall see an illustration for calculating value of an integer.
int a,factorial;
printf("\n enter any number");
factorial =fact(a);
printf ("factorial value =%fctorial);

fact (int x)
int f =1,i;
for(i=x;i>=;i++ )
return f;

And at this point is the output

Enter any number (3 is given as input)
Factorial value =6

at the present we will see the program using recursion

int a,fact;
printf(enter any number");
fact =fact_rec(a);
printf("\nfactorial value =%d",fact);

fact_rec(int x)
int f,i;
if (x==1)
return 1;
return (f);

When the recursive program is executed the recursive function calls aren't executed immediately. Relatively, they are placed on a stack until the condition that terminates the recursion is encountered and the function calls are then executed in reverse order, as they are "popped" off the stack. Therefore, when evaluating a factorial recursively, the function calls will proceed in the following order.

n! = n! * (n-1)!
(n-1)! = (n-1)*(n-2)!
(n-2)! = (n-2)*(n-3)!
2! = 2*1!
1! =1
The actual values will afterward be returned in the following reverse order

1! = 1
2! = 2*1! = 2*1 = 2
3! = 3*2! = 3*2 = 6
n! = n * (n-1)!

This reversal in the order of execution is a characteristic of all the functions that are executed recursively.

The recursive function fact_rec() is evaluate as illustrated in the following table.
Function call Value returned
fact_rec(1) 1
fact_rec(2) 2*fact_rec(1) or 2*1
fact_rec(3) 2*fact_rec(2) or 3*2* 1
fact_rec(4) 2*fact_rec(3) or 4*3*2*1

In the first run when the value of x = 1 it verify the if condition if (x=1) is satisfied and is returned through return statement f =x*fact_rec(x-l). Thus this becomes f =2*fact_rec(1). We know that fact_rec( 1) is 1, therefore the expression reduces to (2 * 1) or 2.

Recursive functions can be resourcefully used to solve problems where the solution is expressed in terms of successively applying the same solution to subsets of the problem when we use recursive functions, we must have an if statement, somewhere to force the function to return without recursive call being executed. Otherwise, the function will never return.

Posted Date: 6/11/2013 6:17:31 AM | Location : United States

Related Discussions:- Write a program for calculating value of an integer, Assignment Help, Ask Question on Write a program for calculating value of an integer, Get Answer, Expert's Help, Write a program for calculating value of an integer Discussions

Write discussion on Write a program for calculating value of an integer
Your posts are moderated
Related Questions
At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

What are the various types of control structures in programming? - Primarily there are 3 types of control structures in programming: Sequence, Selection and Repetition. - Se

What is the difference among const char *myPointer and char *const myPointer?  A: Const char *myPointer is a non constant pointer to constant data; whereas char *const myPointer

Insert values in queue - C++ program: Write a program to insert values in queue. int main()   {         int k;     Queue timeLine;     cout     for(

A string S is said to be "Super ASCII", if it contains the character frequency equal to their ascii values. String will contain only lower case alphabets (''a''-''z'') and the asci

Explain procedural logic A procedure, or function, was explained as a set of specific instructions implemented in sequential manner. The data is kept separate from the procedur

Project Description: I am seeking someone who can make this modem work with Linux Caller ID on Ubuntu present stable version. Skills required are C Programming, Python

What is a newline escape sequence? - A newline escape sequence is signified by the \n character. - It is used to insert a new line whereas displaying the output data. - T

write aprogram for random -search to implement if a[i]=x;then terminate other wise continue the search by picking new randon inex into a