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

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
 

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

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

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Dernier (20)

Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
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
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

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