Course
Data Frames
Convert a List to a DataframeCreate an Empty DataframeCombine Two Dataframe into OneChange Column Name of a DataframeExtract Columns From a DataframeDrop Columns in a DataframeReorder Columns in a DataframeSplit DataframeMerge Multiple DataframesDelete Rows From DataframeMake a List of DataframesIntroduction
"Hello World" ProgramAdd Two VectorsFind Sum, Mean and Product of Vector in R ProgrammingTake Input From UserGenerate Random Number from Standard DistributionsSample from a PopulationFind Minimum and MaximumSort a VectorStrings
Concatenate Two StringsFind the Length of a StringCheck if Characters are Present in a StringExtract n Characters From a StringReplace Characters in a StringCompare two StringsConvert Factors to CharactersTrim Leading and Trailing WhitespacesVectors
Concatenate a Vector of StringsCheck if a Vector Contains the Given ElementCount the Number of Elements in a VectorFind Index of an Element in a VectorAccess Values in a VectorAdd Leading Zeros to VectorJavaScript Program to Display Fibonacci Sequence Using Recursion
To understand this example, you should have the knowledge of the following JavaScript programming topics:
A fibonacci sequence is written as:
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
The Fibonacci sequence is the integer sequence where the first two terms are 0 and 1. After that, the next term is defined as the sum of the previous two terms. Hence, the nth term is the sum of (n-1)th term and (n-2)th term.
Example: Fibonacci Sequence Upto nth Term using Recursion
// program to display fibonacci sequence using recursion
function fibonacci(num) {
if(num < 2) {
return num;
}
else {
return fibonacci(num-1) + fibonacci(num - 2);
}
}
// take nth term input from the user
const nTerms = prompt('Enter the number of terms: ');
if(nTerms <=0) {
console.log('Enter a positive integer.');
}
else {
for(let i = 0; i < nTerms; i++) {
console.log(fibonacci(i));
}
}
Output
Enter the number of terms: 5
0
1
1
2
3
In the above program, a recursive function fibonacci()
is used to find the fibonacci sequence.
- The user is prompted to enter a number of terms up to which they want to print the Fibonacci sequence(here 5).
- The if…else statement is used to check if the number is greater than 0.
- If the number is greater than 0, a for loop is used to calculate each term recursively (calls the
fibonacci()
function again).
Also Read: