FACTORIAL OF A NUMBER USING RECRSION AND WITHOUT RECURSION

#include<stdio.h> #include<conio.h> int fact(int n); int factrec(int n); main() {      int n,ch,y;      clrscr()...

#include<stdio.h>
#include<conio.h>
int fact(int n);
int factrec(int n);
main()
{
     int n,ch,y;
     clrscr();
     printf("ENTER ANY NUMBER\n");
     scanf("%d",&n);
     printf("1:USING WITH OUT RECURSION\N2:USING RECURSION\n");
     printf("ENTERYOURCHOICE\n");
     scanf("%d",&ch);
     switch(ch)
            {
                         case 1:
                                                            y=fact(n);
                                                            break;
                         case 2:
                                                            y=factrec(n);
                                                             break;
                         default:
                                                            printf("INVALID");
                                                             break;
             }
     printf("FACTORIAL OF GIVEN %d NUMBERIS%d",n,y);
     getch();
 }

 int fact(int n)
{
                         int i,fact=1;
                         for(i=1;i<=n;i++)
                        {
                                     fact=fact*i;
                        }
                         return fact;
}

 int factrec(int n)
 {
                         if(n==1)
                         return 1;
                         else
                         return(n*fact(n-1));
  }


OUTPUT:

recursion


Related

C Programs 472981136224481014

Post a Comment

emo-but-icon

item