Program to find Nth odd Fibonacci Number

Given an integer N. The task is to find the Nth odd Fibonacci number.

The odd number fibonacci series is as:

1, 1, 3, 5, 13, 21, 55, 89, 233, 377, 987, 1597………….and so on.



Note: In the above series we have omited even terms from the general fibonacci sequence.

Examples:

Input: N = 3
Output: 3

Input: N = 4
Output: 5

Approach:
On observing carefully, it can be deduced that every third Fibonacci number is even, so the Nth odd Fibonacci number is the {(3*N+1)/2}th term in the general Fibonacci sequence.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for Nth odd fibonaci number
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to find nth odd fibonaci number
int oddFib(int n)
{
    n = (3 * n + 1) / 2;
    int a = -1, b = 1, c, i;
    for (i = 1; i <= n; i++) {
        c = a + b;
        a = b;
        b = c;
    }
    return c;
}
  
// Driver Code
int main()
{
    int n = 4;
      
    cout << oddFib(n);
      
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for Nth odd fibonaci number 
class GFG 
{
      
    // Function to find nth odd fibonaci number 
    static int oddFib(int n) 
    
        n = (3 * n + 1) / 2
          
        int a = -1, b = 1, c = 0, i; 
          
        for (i = 1; i <= n; i++) 
        
            c = a + b; 
            a = b; 
            b = c; 
        
        return c; 
    
      
    // Driver Code 
    public static void main (String[] args) 
    
        int n = 4
          
        System.out.println(oddFib(n)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for Nth odd fibonaci number
  
# Function to find nth odd fibonaci number
def oddFib(n):
    n = (3 * n + 1) // 2
  
    a = -1
    b = 1
    c = 0
    for i in range(1, n + 1):
        c = a + b
        a = b
        b = c
  
    return c
  
# Driver Code
n = 4
  
print(oddFib(n))
  
# This code is contributed by mohit kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for Nth odd fibonaci number 
using System;
      
class GFG 
{
      
    // Function to find nth odd fibonaci number 
    static int oddFib(int n) 
    
        n = (3 * n + 1) / 2; 
          
        int a = -1, b = 1, c = 0, i; 
          
        for (i = 1; i <= n; i++) 
        
            c = a + b; 
            a = b; 
            b = c; 
        
        return c; 
    
      
    // Driver Code 
    public static void Main (String[] args) 
    
        int n = 4; 
          
        Console.WriteLine(oddFib(n)); 
    
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

5

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.