Skip to content

Commit 63cf065

Browse files
authored
Merge pull request #602 from kamalmoussa1/master
Added NthFibonacci.cpp
2 parents 042ded2 + db86335 commit 63cf065

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

NthFibonacci/NthFibonacci.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
3+
/* Function declaration */
4+
int getNthValue(int n);
5+
6+
/* Program starts here */
7+
int main()
8+
{
9+
int nth; // the Nth number
10+
std::cout << "Enter the number of term to be printed: " << std::endl;
11+
12+
std::cin >> nth;
13+
std::cout << getNthValue(nth) << std::endl;
14+
15+
}
16+
17+
/* Function definition */
18+
int getNthValue(int n)
19+
{
20+
/* Initial values */
21+
int a = 1;
22+
int b = 0;
23+
int c;
24+
25+
/* Calculate the sequence untill the Nth number */
26+
for (int i = 0; i <n; i++)
27+
{
28+
c = a + b;
29+
a = b;
30+
b = c;
31+
}
32+
return c; // return the desired number
33+
}

0 commit comments

Comments
 (0)