SlideShare ist ein Scribd-Unternehmen logo
1 von 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;

}

Weitere ähnliche Inhalte

Was ist angesagt?

New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
Syed Umair
 

Was ist angesagt? (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
 

Ähnlich wie week-4x

Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
mustkeem khan
 
C basics
C basicsC basics
C basics
MSc CST
 
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
Mainak 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 number
Mainak Sasmal
 
New microsoft office word document
New microsoft office word documentNew microsoft office word document
New microsoft office word document
Syed Umair
 

Ähnlich wie 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
 

Mehr von 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
 

Kürzlich hochgeladen

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Kürzlich hochgeladen (20)

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
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
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

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; }