Wednesday 12 September 2012

Program that use non 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
Ex: 5! =5*4*3*2*1= 120
Algorithm: main program
Step 1: start
Step 2: read n
Step 3: call the sub program fact(n)
Step 4: print the f value
Step 5: stop
            Sub program:
Step 1: initialize the f=1
Step 2: if n==0 or n=1 return 1 to main program. If not goto step 3
Step 3: perform the looping operation as follows
                        For i=1 i<=n; i++
Step 4: f=f*i
Step 5: return f value to the main program
 
 Program:

#include<stdio.h>
#include<conio.h>
int fact(int n)   //starting of the sub program
 {
              int f=1,i;
              if((n==0)||(n==1))  // check the condition for n value
              return(1);
              else
            for(i=1;i<=n;i++)  // perform the looping operation for calculating the factorial
              f=f*i;
  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: 7
  Factorial of number: 5040
2. Enter the number: 6
  Factorial of number: 720
3. Enter the number: 8
   Factorial of number: -25216
Conclusion:
The program is error free

VIVA QUESATIONS:

1)      What is meant by call by value ?
Ans:  passing values to the function as arguments
2) What is meant by call by reference ?
Ans: passing address to the function as  arguments
3)define actual parameters ?
Ans: The actual parameters  often known as arguments are specified  in the function call.


No comments:

Post a Comment