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

Assignment 1

The document discusses recursion and provides examples of writing recursive functions in C++. It introduces recursion as a technique where a function calls itself, and provides examples of writing recursive functions to calculate the factorial of a number, find the nth Fibonacci number, and provides additional practice problems for writing recursive functions to calculate things like the sum of the squares of the first n positive integers and the sum of the first n powers of a base number.

Uploaded by

ansar20042009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Assignment 1

The document discusses recursion and provides examples of writing recursive functions in C++. It introduces recursion as a technique where a function calls itself, and provides examples of writing recursive functions to calculate the factorial of a number, find the nth Fibonacci number, and provides additional practice problems for writing recursive functions to calculate things like the sum of the squares of the first n positive integers and the sum of the first n powers of a base number.

Uploaded by

ansar20042009
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Algorithms and Data Structures

Assignment 1: Recursion
Recursion is the technique of making a function call itself. This technique
provides a way to break complicated problems down into simple problems which
are easier to solve.

Example 1:
Write and test a recursive function that calculates the factorial of a given integer.
The factorial of the number N is calculated using the following formula:
N!=1·2·3···N

#include <iostream>
using namespace std;

/*
выход из рекурсии
1. 1! = 1
0! = 1
*/

/*
2. Формула рекурсии n! = n * (n-1)!
*/
/*

int Fact(int n) {
if (n == 0) {
return 1;
}
else {
return n * Fact(n - 1);
}
}

int main()
{
setlocale(LC_ALL, "Russian");
int x = 3;
cout << Fact(x);
}

Example 2:
Write and test a recursive function to find the Fibonacci number.
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987............
#include <iostream>
using namespace std;

int fib(int number) {

if (number < 1) {
return 0;
}

if (number == 1) {
return 1;
}

return fib(number - 1) + fib(number - 2);


}

int main()
{
setlocale(LC_ALL, "Russian");

int res = fib(8);

cout << "Fib = " << res << endl;

1. Write and test a recursive function that returns the sum of the squares of the
first n positive integers.
12 + 22 + … + 𝑛2
Sample Input: n = 4
Sample Output: 12 + 22 + 32 + 42 = 30
2. Write and test a recursive function that returns the sum of the first n
elements of an array.
3. Write a recursive method to compute the sum of the first n positive integers.
4. Write and test a recursive function that returns the sum of the first n powers
of a base b.
𝑏0 + 𝑏1 + 𝑏2 + … + 𝑏𝑛
Sample Input: b = 4, n = 3
Sample Output: 40 + 41 + 42 + 43 = 85
5. Write and test a recursive function that returns the sum of a given
formula
1 + 1 / (1!) + 1 / (2!) + 1 / (3!) + … + 1/(N!)

You might also like