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

Fibonacci Tutorial

The document provides examples of 2D and 3D arrays in C++, demonstrating how to initialize and access their elements. It also includes a tutorial on the Fibonacci sequence, explaining its definition and providing a C program that calculates the nth Fibonacci number using recursion. The program includes input validation for negative numbers and outputs the result based on user input.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Fibonacci Tutorial

The document provides examples of 2D and 3D arrays in C++, demonstrating how to initialize and access their elements. It also includes a tutorial on the Fibonacci sequence, explaining its definition and providing a C program that calculates the nth Fibonacci number using recursion. The program includes input validation for negative numbers and outputs the result based on user input.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Array examples

2D
#include <iostream>
using namespace std;
int main() {
int m[3][2]={{2,-5},{4,0},{9,1}}; //2-D
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 2; ++j)
{ cout<<“m["<<i<<"]
["<<j<<"]=
"<<m[i][j] <<endl;
}
}
return 0; }
3D
int main() {
int n[2][3][2]; // 3-D array
cout<<“enter 12 values: \n";
for(int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for(int k = 0; k < 2; ++k ) {
cin>>n[i][j][k];
}
}
}
for(int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for(int k = 0; k < 2; ++k ) {
cout<<“n["<<i<<"]["<<j<<"]["<<k<<"]
= " << n[i][j][k] << endl;
}
}
} return 0; }
Fibonacci tutorial

The Fibonacci sequence is a series of numbers where the first


number is zero(0) and the second number is one(1). The
subsequent numbers are computed from the sum of the previous
two numbers in the sequence.

The pattern is 0, 1, 1, 2, 3, 5, 8, ...


1. /*
2. * C Program to find the nth number in Fibonacci series using recursion
3. */
4.
5. #include <stdio.h>
6. int fibo(int);
7.
8. int main()
9. {
10. int num;
11. int result;
12.
13. printf("Enter the nth number in fibonacci series: ");
14. scanf("%d", &num);
15. if (num < 0)
16. {
17. printf("Fibonacci of negative number is not possible.\n");
18. }
19. else
20. {
21. result = fibo(num);
22. printf("The %d number in fibonacci series is %d\n", num, result);
23. }
24. return 0;
25. }
26.
27. int fibo(int num)
28. {
29. if (num == 0)
30. {
31. return 0;
32. }
33. else if (num == 1)
34. {
35. return 1;
36. }
37. else
38. {
39. return(fibo(num - 1) + fibo(num - 2));
40. }
41. }

You might also like