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

Lab ASSIGNMENT-IV (BCAC 393)

The document contains 6 programs written as assignments for a Data Structures and Algorithms lab course. Each program demonstrates a different recursion concept: 1) Factorial calculation, 2) Fibonacci series, 3) GCD, 4) Exponent calculation, 5) Tower of Hanoi problem, and 6) Combination calculation. For each program, the code is provided and an output section is included but left blank, indicating the expected output was not included.

Uploaded by

Gourav Jain
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 views14 pages

Lab ASSIGNMENT-IV (BCAC 393)

The document contains 6 programs written as assignments for a Data Structures and Algorithms lab course. Each program demonstrates a different recursion concept: 1) Factorial calculation, 2) Fibonacci series, 3) GCD, 4) Exponent calculation, 5) Tower of Hanoi problem, and 6) Combination calculation. For each program, the code is provided and an output section is included but left blank, indicating the expected output was not included.

Uploaded by

Gourav Jain
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/ 14

Calcutta Institute of Engineering and

Management
24/1A, Chandi Ghosh Road, Tollygunge, Kolkata-
700040

Stream: BCA Year: 2nd Semester:3rd

Paper Name: Data Structure & Algorithm Lab


Paper Code: BCAC393

Name GOURAV JAIN

University Roll 29901220025


Number
Data Structure & Algorithm Lab (BCAC393)
2021
ROLL 29901220025
NAME GOURAV JAIN
Learning outcome: Students will be able to perform different
recursion operations.

ASSIGNMENT: IV

Sl. Program Listing


No
1 Write a tail recursion function to find out the Factorial of a given number.
2 Write a recursion function to display Fibonacci series.
3 Write a recursion function to find out the GCD of two given numbers.

4 Write a recursion function to find out x to the power n where x and n are two given
numbers.
5 Write a recursion function to implement Tower of Hanoi problem of n disks.
6. Write a recursion function to find out combination nCk
Program:- 1
Write a tail recursion function to find out the Factorial of a given number.

Code-
#include<stdio.h>
int factorial( int n, int fact )
{
if ( n==1 )
return fact;
else
factorial( n-1, n*fact );
}

//main function to test above function


int main( ){
int n,value;

//input an integer number


printf( "Enter the number : " );
scanf( "%d", &n );

if ( n < 0 )
printf( "No factorial of negative number\n" );
else if ( n==0 )
printf( "Factorial of zero is 1\n" );
else
{
value = factorial( n,1 );
printf( "Factorial of %d = %d\n",n,value );
}

return 0;
}
Output-
Program-2
Write a recursion function to display Fibonacci series.
Code-
#include <stdio.h>
int n,count,t1,t2,t3;
int fibo (int a, int b);
int main()
{
printf ("\n How many no of terms to be printed?");
scanf("%d",&n);
t1=0;
t2=1;
printf("\n The first %d term in Fibonacci in Fibonacci series are \n",n);
printf(" %d %d ",t1,t2);
count=2;
fibo(t1,t2);

}
int fibo(int t1,int t2)
{
if(count>=n)
return 0;
else
{
t3=t1+t2;
printf("%5d",t3);
count++;
t1=t2;
t2=t3;
fibo(t1,t2);

}
}
Output-
Program-3

Write a recursion function to find out the GCD of two given numbers.
Code-
#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}

int hcf(int n1, int n2) {


if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
Output-
Program-4

Write a recursion function to find out x to the power n where x and n


are two given numbers.
Code-
#include <stdio.h>
int power(int n1, int n2);
int main()
{
int x, n, result;
printf("Enter x number: ");
scanf("%d", &x);
printf("Enter power number: ");
scanf("%d", &n);
result = power(x, n);
printf("%d^%d = %d", x, n, result);
return 0;
}

int power(int x, int n)


{
if (n != 0)
return (x * power(x, n - 1));
else
return 1;
}
Output-
Program-5
Write a recursion function to implement Tower of Hanoi problem of n
disks
Code-
#include <stdio.h>

void towers(int, char, char, char);

int main()
{
int num;

printf("Enter the number of disks : ");


scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are
:\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg,
topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}
Output-
Program-6
Write a recursion function to find out combination nCk
Code-
#include <stdio.h>
int nCr(int n, int r)
{
if(r==0||r==n)
return 1;
else
return nCr(n-1,r-1)+nCr(n-1,r);

}
int main()
{

int n,r;

printf("\n Enter the value to n and r :");


scanf("%d%d",&n,&r);

printf("\n Value of nCr = %d \n",nCr(n,r));

}
Output-

-----------------------------------------x------------------------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy