SlideShare une entreprise Scribd logo
1  sur  8
/* Write C programs that use both recursive and non-recursive functions

    To solve Towers of Hanoi problem.*/



#include<conio.h>

#include<stdio.h>



/* Non-Recursive Function*/

void hanoiNonRecursion(int num,char sndl,char indl,char dndl)

{

    char stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;

    int top,add;

    top=NULL;



    one:

           if(num==1)

           {

               printf("nMove top disk from needle %c to needle %c ",sndl,dndl);

               goto four;

           }

    two:

           top=top+1;

           stkn[top]=num;

           stksndl[top]=sndl;

           stkindl[top]=indl;
stkdndl[top]=dndl;

         stkadd[top]=3;

         num=num-1;

         sndl=sndl;

         temp=indl;

         indl=dndl;

         dndl=temp;



         goto one;



three:

         printf("nMove top disk from needle %c to needle %c ",sndl,dndl);

         top=top+1;

         stkn[top]=num;

         stksndl[top]=sndl;

         stkindl[top]=indl;

         stkdndl[top]=dndl;

         stkadd[top]=5;

         num=num-1;

         temp=sndl;

         sndl=indl;

         indl=temp;

         dndl=dndl;
goto one;



    four:

            if(top==NULL)

             return;

            num=stkn[top];

            sndl=stksndl[top];

            indl=stkindl[top];

            dndl=stkdndl[top];

            add=stkadd[top];

            top=top-1;

            if(add==3)

             goto three;

            else if(add==5)

             goto four;

}



/* Recursive Function*/

void hanoiRecursion( int num,char ndl1, char ndl2, char ndl3)

{

     if ( num == 1 ) {

            printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );

            return;

     }
hanoiRecursion( num - 1,ndl1, ndl3, ndl2 );

      printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );

      hanoiRecursion( num - 1,ndl3, ndl2, ndl1 );

}



void main()

{

    int no;

    clrscr();

    printf("Enter the no. of disks to be transferred: ");

    scanf("%d",&no);



    if(no<1)

      printf("nThere's nothing to move.");

    else

      printf("Non-Recursive");

      hanoiNonRecursion(no,'A','B','C');

      printf("nRecursive");

      hanoiRecursion(no,'A','B','C');



    getch();

}
/* Write C programs that use both recursive and non-recursive functions

    To find the GCD (greatest common divisor) of two given integers.*/



#include<stdio.h>

#include<conio.h>

#include<math.h>



unsigned int GcdRecursive(unsigned m, unsigned n);

unsigned int GcdNonRecursive(unsigned p,unsigned q);



int main(void)

{

    int a,b,iGcd;

    clrscr();



    printf("Enter the two numbers whose GCD is to be found: ");

    scanf("%d%d",&a,&b);



    printf("GCD of %d and %d Using Recursive Function is %dn",a,b,GcdRecursive(a,b));

    printf("GCD of %d and %d Using Non-Recursive Function is %dn",a,b,GcdNonRecursive(a,b));
getch();

}



/* Recursive Function*/

unsigned int GcdRecursive(unsigned m, unsigned n)

{

if(n>m)

       return GcdRecursive(n,m);

if(n==0)

        return m;

else

     return GcdRecursive(n,m%n);

}



/* Non-Recursive Function*/

unsigned int GcdNonRecursive(unsigned p,unsigned q)

{

unsigned remainder;

remainder = p-(p/q*q);



if(remainder==0)

     return q;

else
GcdRecursive(q,remainder);

}




/* Write C programs that use both recursive and non-recursive functions

    To find the factorial of a given integer.*/



#include<stdio.h>

#include<conio.h>



unsigned int recr_factorial(int n);

unsigned int iter_factorial(int n);



void main()

{

    int n,i;

    long fact;

    clrscr();

    printf("Enter the number: ");

    scanf("%d",&n);



    if(n==0)
printf("Factorial of 0 is 1n");

    else

    {

        printf("Factorial of %d Using Recursive Function is %dn",n,recr_factorial(n));

        printf("Factorial of %d Using Non-Recursive Function is %dn",n,iter_factorial(n));

    }

    getch();

}



/* Recursive Function*/

unsigned int recr_factorial(int n) {

        return n>=1 ? n * recr_factorial(n-1) : 1;

}



/* Non-Recursive Function*/

unsigned int iter_factorial(int n) {

        int accu = 1;

        int i;

        for(i = 1; i <= n; i++) {

                 accu *= i;

        }

        return accu;

}

Contenu connexe

Tendances (20)

StackArray stack3
StackArray stack3StackArray stack3
StackArray stack3
 
week-15x
week-15xweek-15x
week-15x
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Odd number
Odd numberOdd number
Odd number
 
week-16x
week-16xweek-16x
week-16x
 
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
 
Shan
ShanShan
Shan
 
week-17x
week-17xweek-17x
week-17x
 
One dimensional operation of Array in C- language
One dimensional operation of Array in C- language One dimensional operation of Array in C- language
One dimensional operation of Array in C- language
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings
 
Final ds record
Final ds recordFinal ds record
Final ds record
 
week-21x
week-21xweek-21x
week-21x
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Adding two integers in c
Adding two integers in cAdding two integers in c
Adding two integers in c
 
C program to implement linked list using array abstract data type
C program to implement linked list using array abstract data typeC program to implement linked list using array abstract data type
C program to implement linked list using array abstract data type
 

Similaire à week-4x

Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020vrgokila
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresLakshmi Sarvani Videla
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfAshutoshprasad27
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxAshutoshprasad27
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...DR B.Surendiran .
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++mustkeem khan
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diplomamustkeem khan
 
C basics
C basicsC basics
C basicsMSc CST
 
Program flowchart
Program flowchartProgram flowchart
Program flowchartSowri Rajan
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using CBilal Mirza
 
New microsoft office word document
New microsoft office word documentNew microsoft office word document
New microsoft office word documentSyed Umair
 

Similaire à week-4x (20)

Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
cpract.docx
cpract.docxcpract.docx
cpract.docx
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
7 functions
7  functions7  functions
7 functions
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
C basics
C basicsC basics
C basics
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Lab Question
Lab QuestionLab Question
Lab Question
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
New microsoft office word document
New microsoft office word documentNew microsoft office word document
New microsoft office word document
 

Plus de KITE www.kitecolleges.com (20)

DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENTDISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
 
BrainFingerprintingpresentation
BrainFingerprintingpresentationBrainFingerprintingpresentation
BrainFingerprintingpresentation
 
ch6
ch6ch6
ch6
 
PPT (2)
PPT (2)PPT (2)
PPT (2)
 
ch14
ch14ch14
ch14
 
ch16
ch16ch16
ch16
 
holographic versatile disc
holographic versatile discholographic versatile disc
holographic versatile disc
 
week-22x
week-22xweek-22x
week-22x
 
week-5x
week-5xweek-5x
week-5x
 
week-3x
week-3xweek-3x
week-3x
 
ch8
ch8ch8
ch8
 
Intro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.ukIntro Expert Systems test-me.co.uk
Intro Expert Systems test-me.co.uk
 
ch17
ch17ch17
ch17
 
ch4
ch4ch4
ch4
 
week-7x
week-7xweek-7x
week-7x
 
week-9x
week-9xweek-9x
week-9x
 
week-14x
week-14xweek-14x
week-14x
 
AIRBORNE
AIRBORNEAIRBORNE
AIRBORNE
 
ch2
ch2ch2
ch2
 
week-23x
week-23xweek-23x
week-23x
 

Dernier

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 

Dernier (20)

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 

week-4x

  • 1. /* Write C programs that use both recursive and non-recursive functions To solve Towers of Hanoi problem.*/ #include<conio.h> #include<stdio.h> /* Non-Recursive Function*/ void hanoiNonRecursion(int num,char sndl,char indl,char dndl) { char stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp; int top,add; top=NULL; one: if(num==1) { printf("nMove top disk from needle %c to needle %c ",sndl,dndl); goto four; } two: top=top+1; stkn[top]=num; stksndl[top]=sndl; stkindl[top]=indl;
  • 2. stkdndl[top]=dndl; stkadd[top]=3; num=num-1; sndl=sndl; temp=indl; indl=dndl; dndl=temp; goto one; three: printf("nMove top disk from needle %c to needle %c ",sndl,dndl); top=top+1; stkn[top]=num; stksndl[top]=sndl; stkindl[top]=indl; stkdndl[top]=dndl; stkadd[top]=5; num=num-1; temp=sndl; sndl=indl; indl=temp; dndl=dndl;
  • 3. goto one; four: if(top==NULL) return; num=stkn[top]; sndl=stksndl[top]; indl=stkindl[top]; dndl=stkdndl[top]; add=stkadd[top]; top=top-1; if(add==3) goto three; else if(add==5) goto four; } /* Recursive Function*/ void hanoiRecursion( int num,char ndl1, char ndl2, char ndl3) { if ( num == 1 ) { printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 ); return; }
  • 4. hanoiRecursion( num - 1,ndl1, ndl3, ndl2 ); printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 ); hanoiRecursion( num - 1,ndl3, ndl2, ndl1 ); } void main() { int no; clrscr(); printf("Enter the no. of disks to be transferred: "); scanf("%d",&no); if(no<1) printf("nThere's nothing to move."); else printf("Non-Recursive"); hanoiNonRecursion(no,'A','B','C'); printf("nRecursive"); hanoiRecursion(no,'A','B','C'); getch(); }
  • 5. /* Write C programs that use both recursive and non-recursive functions To find the GCD (greatest common divisor) of two given integers.*/ #include<stdio.h> #include<conio.h> #include<math.h> unsigned int GcdRecursive(unsigned m, unsigned n); unsigned int GcdNonRecursive(unsigned p,unsigned q); int main(void) { int a,b,iGcd; clrscr(); printf("Enter the two numbers whose GCD is to be found: "); scanf("%d%d",&a,&b); printf("GCD of %d and %d Using Recursive Function is %dn",a,b,GcdRecursive(a,b)); printf("GCD of %d and %d Using Non-Recursive Function is %dn",a,b,GcdNonRecursive(a,b));
  • 6. getch(); } /* Recursive Function*/ unsigned int GcdRecursive(unsigned m, unsigned n) { if(n>m) return GcdRecursive(n,m); if(n==0) return m; else return GcdRecursive(n,m%n); } /* Non-Recursive Function*/ unsigned int GcdNonRecursive(unsigned p,unsigned q) { unsigned remainder; remainder = p-(p/q*q); if(remainder==0) return q; else
  • 7. GcdRecursive(q,remainder); } /* Write C programs that use both recursive and non-recursive functions To find the factorial of a given integer.*/ #include<stdio.h> #include<conio.h> unsigned int recr_factorial(int n); unsigned int iter_factorial(int n); void main() { int n,i; long fact; clrscr(); printf("Enter the number: "); scanf("%d",&n); if(n==0)
  • 8. printf("Factorial of 0 is 1n"); else { printf("Factorial of %d Using Recursive Function is %dn",n,recr_factorial(n)); printf("Factorial of %d Using Non-Recursive Function is %dn",n,iter_factorial(n)); } getch(); } /* Recursive Function*/ unsigned int recr_factorial(int n) { return n>=1 ? n * recr_factorial(n-1) : 1; } /* Non-Recursive Function*/ unsigned int iter_factorial(int n) { int accu = 1; int i; for(i = 1; i <= n; i++) { accu *= i; } return accu; }