Find Nth Fibonacci Number
Nth term of Fibonacci series F(n), where F(n) is a function, is calculated using the following formula -
pascal triangle fibonacci |
C/C++,java,python c++ keywords, identifiers, data types,variables,modifiers,storage classes,operators,for loop ,while loop,do while loop,if else statement,nested loops,break continue goto statement,functions ,call by value call by reference ,arrays ,strings,pointers,data structures ,classes and objects,inheritance,polymorphism,operator overloading,function overloading,data abstraction,data encapsulation,dynamic memory allocation for array and objects,function templates,class templates,
Find Nth Fibonacci Number
pascal triangle fibonacci |
F(n) = F(n-1) + F(n-2),
Where, F(1) = F(2) = 1
The first line of each test case contains a real number ‘N’.
For each test case, return its equivalent Fibonacci number.
1 <= N <= 10000
Where ‘N’ represents the number for which we have to find its equivalent Fibonacci number.
Time Limit: 1 second
6
8
Now the number is ‘6’ so we have to find the “6th” Fibonacci number
So by using the property of the Fibonacci series i.e
[ 1, 1, 2, 3, 5, 8]
So the “6th” element is “8” hence we get the output.
C++ Code:
#include <iostream>
using namespace std;
int main()
{
int c,n;
int a=1;
int b=1;
cin>>n;
if (n<=2)
{
cout << 1;
}
else
{
for(int i=3;i<=n;i++)
{
c = a+b;
a = b;
b = c;
if(i==n)
{
cout<<c<<" ";
break ;
}
else
continue ;
}
}
}
0 comments:
Post a Comment