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

Assignment Help:

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.
main()
{
int a,factorial;
printf("\n enter any number");
scanf("%d",&a);.
factorial =fact(a);
printf ("factorial value =%fctorial);
}

fact (int x)
{
int f =1,i;
for(i=x;i>=;i++ )
f=f*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

main()
{
int a,fact;
printf(enter any number");
scanf("%d",&a);
fact =fact_rec(a);
printf("\nfactorial value =%d",fact);
}

fact_rec(int x)
{
int f,i;
if (x==1)
return 1;
else
f=x*fact_rec(x-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.


Related Discussions:- Write a program for calculating value of an integer

State six typical application of primary cells, Normal 0 false ...

Normal 0 false false false EN-IN X-NONE X-NONE MicrosoftInternetExplorer4

What operators can or cannot be overloaded?, A: Mostly can be overloaded. T...

A: Mostly can be overloaded. The only C operators which can't be are. and?: (and sizeof, that is technically an operator). C++ adds a few of its own operators, mostly which can be

Give a formal expression of the specification, A function REPAT is specifie...

A function REPAT is specified below. Function REPAT(c in Char, i in Int, s in mString) return in mString pre 1 ≤ i ≤ the length of s. post The returned value is a string identic

Is there anything you can do in c++ which you cannot do in c, A: No. There ...

A: No. There is nothing you can do in C++ which you cannot do in C. In spite of everything

Write a program to illustrate array, Write a Program to illustrate Array? ...

Write a Program to illustrate Array? int x[100]; char text[80]; float temp[30]; static int marks[5]; We are able to use symbolic constants instead of expression. The valu

What are pre-processor directives, What are pre-processor directives? -...

What are pre-processor directives? - Pre-processor directives are placed at the beginning of a C program. They begin with # symbol. - This is the place, where library files

Graphical user interface designed for the output peripherals, Introduction ...

Introduction to I/O interfacing, discuss microcontrollers applications in industry. Detailed product design specifications that have sections on both hardware and software inclu

Rules of operator overloading, Rules of Operator Overloading It is ...

Rules of Operator Overloading It is a function defined to an operator with new term or meaning. It cannot produce new operator. It cannot modified the meaning of th

I need voting bot, Project Description: Seeking a Voting Bot Script / Pr...

Project Description: Seeking a Voting Bot Script / Program Skills required are ASP, Javascript, C Programming, PHP, Software Architecture

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd