Wednesday, August 17, 2011

QUES 19 FACTORIAL Recursive and Non Recursive.


C LANGUAGE BOOK



Factorial of 2 = 2 *1=2
Factorial of 3 = 3*2 *1=6
Factorial of 4 = 4*3*2 *1=24
Factorial of 5 = 5*4*3*2 *1=120

WAP TO PRINT THE  FACTORIAL UPTO N NUMBER USING RECURSION AND NON RECURSION.
THE N SHOULD BE ENTER BY THE USER.


#include<stdio.h>

void main()
{
int x, i,j=0;
printf(" ENTER NUMBER UPTO WHICH FACTORIAL HAS TO BE GENERATED___ ");
scanf("%d", &x);
printf(" \n FACTORIAL ARE USING RECURSION");

for(i = 1; i < x; i++)
{
j=FACTR(i);
printf(" \n %d ",j);
}



j=0;


printf(" \n FACTORIAL ARE USING NON-RECURSION");

for(i = 1; i < x; i++)
{
j=FACTNR(i);
printf(" \n %d ",j);
}
getch();
}

/*Recursive*/ 
FACTR(int n)
{
   int t;

   if(n == 1)
     return(1);
  
    t = FACTR(n-1)*n;      /*Recursive call*/
     return (t);
}


/*Non-Recursive*/ 

FACTNR(int n)
{

int i,t;
t=1;
for(i=1;i<=n;i++)

    t = t*i;      /*Non-Recursive call*/
  }
return (t);
}








C LANGUAGE BOOK

No comments:

Post a Comment