Wednesday 12 September 2012

Programs that use recursive function to find the factorial of a given integer


Description:
Factorial of a number is nothing but the multiplication of numbers from a given number to 1
Algorithm: main program
Step 1:  start
Step 2: read n
Step 3: call sub program as f=fact(n)
Step 4: print f value
Step 5: stop
 Sub program:
Step 1: initialize the f
Step 2: if n= = 0 or n == 1 return 1 to main program if not goto step 3
Step 3: return n*fact(n-1) to main program

 
Program:

#include<stdio.h>
#include<conio.h>
int fact(int n)
 {
            int f;
            if((n==0)||(n==1))  // check the condition for the  n value
             return(n);
            else
             f=n*fact(n-1);   //calculate the factorial of n
             return(f);
 }
void main()
{
            int n;
            clrscr();
            printf("enter the  number :");
            scanf("%d",&n);
             printf("factoria of number%d",fact(n));
            getch();
}
Output:
1. Enter the number : 5
     Factorial of number: 120
2. Enter the number : 3
     Factorial of number: 6
3. Enter the number : 9
    Factorial of number: -30336
Conclusion: the program is error free


VIVA QUESATIONS:

1)      What is the meaning of factorial  number?
Ans : Factorial of a number is nothing but the multiplication of numbers from a given 
          number to 1
2)      What is the meaning of recusive function ?
Ans:  A function call it self is called recursive function
3)      define library functions ?
Ans: The functions have already been written, compiled and placed in libraries and are   
         called library functions.
4)      Define formal parameters ?
Ans:    Formal parameters  are the parameters given in the function declaration ans  
           function definition.

No comments:

Post a Comment