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

Program for Matrix Addition and Subtraction

# include <stdio.h> #include<conio.h> main ( ) { int a[3][3],b[3][3],c[3][3],d[3][3],i,j; printf(“enter elements into matrix a”); for ( i = 0; i < 3; i + + ) for ( j = 0; j &l...

Program to Find Largest and Smallest numbers using Arrays

# include <stdio.h> #include<conio.h> main () { int a[10],i,big,small,n; printf (“enter the size of the array”); scanf (“%d, & n); printf (“enter elements in an array”); for ...

Write a program for Sorting of Numbers using Functions & Pointer?

#include<stdio.h> #include<conio.h> void sort(int *,int); void main() { int a[10], i, n; printf(“enter how many elements”); scanf(“%d”, &n); printf(“enter elements”); f...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item