0% found this document useful (0 votes)
15 views5 pages

Lab 07

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Lab 07

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DATA STRUCTURES & ALGORITHMS

LAB 7

PREPARED BY

Name ` Abbas Mehdi

Section 6B

Reg. No FA21-BEE-001
TASK 1
#include<stdio.h>
#include<string.h>
#include<stdbool.h>

bool isPalRec (char srt[], int s, int e)


{ if (s==e) return true;
if (srt[s] != srt[e]) return
false; if (s<e+1) return
isPalRec(srt,s+1,e-1); return
true;
}

bool test_palindrome_rec(char my_word[])


{ int n=strlen(my_word);
if(n==0) return true; return
isPalRec(my_word, 0, n-1);

} int
main()
{

char my_word[7]; int choice; printf("this program tells about


the palindrome criteria of word \n"); printf("enter a woird(max 6)");
scanf("%s",&my_word); int i=strlen(my_word);
if(i>6) {
printf("error");
return 0;
} printf("\n recrusive output :");
if(test_palindrome_rec(my_word)==true)
printf("This word is a palindrome \n");
else printf(" not a palindrome \n");
return 0;
}

TASK 2
#include<stdio.h> #include<string.h>
#include<stdbool.h>

int GCD_rec(int x, int y)


{

int rem;

rem = x%y;
if(rem==0)
return y;
else

return (GCD_rec(y, rem));


}

int GCD_itr(int x, int y) { int


gcd; for(int i=1;i<=x &&
i<=y;i++)
{
if(x%i==0 && y%i==0)
{
gcd=i;
} }
return gcd; }
int main() {

int num1, num2, res;

printf("\nEnter two numbers separated by a space: ");

scanf("%d %d", &num1, &num2);

res = GCD_rec(num1, num2); printf("\nRecursive Output: GCD of


%d and %d = %d", num1, num2, res);

res = GCD_itr(num1, num2); printf("\nRecursive Output: GCD of %d


and %d = %d", num1, num2, res);

return 0;
}

Conclusion:
In this I have understand the basics of recursive functions how it implement in program
and know how its work. I have also taught how to convert an iterative solution to a
problem to its recursive counterpart. I learn how to understand the advantages and
disadvantages of recursion. I complete this lab using different concepts of DSA including
recursion and iteration.

You might also like