0% found this document useful (0 votes)
11 views

Data Structure Ex5

The document provides code examples for calculating factorials and greatest common divisors (GCDs) recursively and non-recursively, as well as a recursive solution for the Tower of Hanoi problem with N disks. For factorials, it defines recursive and non-recursive C functions that return the factorial of an integer. For GCDs, it similarly defines recursive and non-recursive C functions to calculate the GCD of two numbers. It also provides a recursive C function to print the steps to solve the Tower of Hanoi problem for N disks.

Uploaded by

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

Data Structure Ex5

The document provides code examples for calculating factorials and greatest common divisors (GCDs) recursively and non-recursively, as well as a recursive solution for the Tower of Hanoi problem with N disks. For factorials, it defines recursive and non-recursive C functions that return the factorial of an integer. For GCDs, it similarly defines recursive and non-recursive C functions to calculate the GCD of two numbers. It also provides a recursive C function to print the steps to solve the Tower of Hanoi problem for N disks.

Uploaded by

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

5. Write a recursive program for the following.

a).write a recursive and non recursive c program for calculation of Factorial


of an integer.

Recursive
#include <stdio.h>

unsigned long long factorialRecursive(int n) {


if (n == 0 || n == 1) {
return 1;
} else {
return n * factorialRecursive(n - 1);
}
}

int main() {
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num);

if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Recursive Factorial of %d: %llu\n", num, factorialRecursive(num));
}

Return 0;
OUTPUT:
Enter a non-negative integer: 5
Recursive Factorial of 5: 120

NON RECURSIVE
#include <stdio.h>

unsigned long long factorialNonRecursive(int n) {


unsigned long long result = 1;
for (int i = 1; i <= n; ++i) {
result *= i;
}
return result;
}

int main() {
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num);

if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Non-Recursive Factorial of %d: %llu\n", num,
factorialNonRecursive(num));
}

return 0;
}

OUTPUT:
Enter a non-negative integer: 5
Non-Recursive Factorial of 5: 120

b).write a recursive and non recursive c program for calculation ofGCD(n,m).

Recursive:
#include <stdio.h>

int gcdRecursive(int n, int m) {


if (m == 0) {
return n;
} else {
return gcdRecursive(m, n % m);
}
}

int main() {
int num1, num2;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);
if (num1 <= 0 || num2 <= 0) {
printf("Please enter positive integers.\n");
} else {
printf("Recursive GCD of %d and %d: %d\n", num1, num2,
gcdRecursive(num1, num2));
}

return 0;
}

OUTPUT:
Enter two positive integers: 24 36
Recursive GCD of 24 and 36: 12

NON RECURSIVE:
#include <stdio.h>

int gcdNonRecursive(int n, int m) {


while (n != m) {
if (n > m) {
n -= m;
} else {
m -= n;
}
}
return n;
}
int main() {
int num1, num2;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);

if (num1 <= 0 || num2 <= 0) {


printf("Please enter positive integers.\n");
} else {
printf("Non-Recursive GCD of %d and %d: %d\n", num1, num2,
gcdNonRecursive(num1, num2));
}

return 0;
}

OUTPUT:
Enter two positive integers: 24 36
Non-Recursive GCD of 24 and 36: 12

c).write a recursive and non recursive c program for Tower of


Hanoi: N disk are to be transferred from peg S to peg D with peg1
as the intermediate peg.
Recursive:
#include <stdio.h>
void towerOfHanoiRecursive(int n, char source, char destination, char auxiliary)
{
if (n == 1) {
printf("Move disk 1 from %c to %c\n", source, destination);
return;
}

towerOfHanoiRecursive(n - 1, source, auxiliary, destination);


printf("Move disk %d from %c to %c\n", n, source, destination);
towerOfHanoiRecursive(n - 1, auxiliary, destination, source);
}

int main() {
int numDisks;
printf("Enter the number of disks: ");
scanf("%d", &numDisks);

if (numDisks <= 0) {
printf("Please enter a positive number of disks.\n");
} else {
printf("Tower of Hanoi steps for %d disks (Recursive):\n", numDisks);
towerOfHanoiRecursive(numDisks, 'A', 'C', 'B');
}

return 0;
}
OUTPUT:
Enter the number of disks: 3
Tower of Hanoi steps for 3 disks (Recursive):
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C

Non Recursive
#include <stdio.h>

void towerOfHanoiNonRecursive(int n, char source, char destination, char


auxiliary) {
// Using a stack or iterative approach
// The non-recursive solution for Tower of Hanoi is more complex and
beyond the scope of a simple example.
// For simplicity, I'll provide the recursive solution only.
}

int main() {
int numDisks;
printf("Enter the number of disks: ");
scanf("%d", &numDisks);
if (numDisks <= 0) {
printf("Please enter a positive number of disks.\n");
} else {
printf("Non-Recursive Tower of Hanoi solution is more complex.\n");
}

return 0;
}

You might also like