The Nth Fibonnaci | Practice | GeeksforGeeks

Given a positive integer N, find the last digit of the Nth term from the Fibonacci series.
Note: For N=0 you have to return 0.
 
Example 1:
Input:
N = 5
Output:
5
Explanation:
5th Fibonacci number is 5
Example 2:
Input:
N = 14
Output:
7
Explanation:
14th Fibonacci number is 377
It's last digit is 7


Your Task:
You don't need to read input or print anything. Your task is to complete the function fib() which takes an integer N as input parameter and returns the last digit of Nth Fibonacci number.

Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
 
Constraints:
0 <= N <= 1000
0
rohitsinha9692 weeks ago
JAVA Solution in 0.12 secs
class Solution{   static int fib(int N){       //code here       if(N==0){           return 0;       }       else {           int n1=0,n2=1,n3=0;       for(int i=0;i<N;i++){           n3=(n1+n2)%10;           n2=n1;           n1=n3;       }       return n3;       }   }}
0
imabhishek023 weeks ago
 int fib(int n){
       if(n==0)
       return 0;
       
       if(n==1)
       return 1;
       
      // if(n==2)
       //return 1;
       
       int a=0;
       int b=1;
       int c;
        
        for(int i=2;i<=n;i++)
        {
            c=(a+b)%10;
            a=b,b=c;
        }return c;
    }
0
imabhishek023 weeks ago
whats wrong ,output showing time limit exceed?
int fibo(int N){
     if(N==0)
    {
    return 0;
    }
    if(N==1)
    return 1;
    
   int ans=fib(N-2)+fib(N-1);
    return ans;
}

    int fib(int N){
        
    int k=fibo(N);
    
    return k%10;
    
    }
};
0
bahubuli1 month ago
best optimized approach with 
time complexity = O(min(N , 61))
space complexity = O(61)...
using the concept that after every 60th 
fibonacci number last digit repeated 
      int fib(int N)
   {
        int fib[61];
        fib[0] = 0;
        fib[1] = 1;
        
        for(int i=2;i<=min(60,N);i++)
        {
            fib[i] = fib[i-1]+fib[i-2];
            fib[i] = fib[i]%10;
        }
      
      return fib[N%60];
   }
    
0
moaslam8261 month ago
c++ easy soln
int fib(int n){
        if(n==0) return 0;
        if(n==1) return 1;
        if(n==2) return 1;
        int a=1,b=1,c;
        for(int i=3;i<=n;i++){
            c=(a+b)%10;
            a=b; b=c;
        }
        return c;
    }
+1
badgujarsachin835 months ago
int fib(int N){
        //code here
       if(N==1){
           return 1;
       }
       int a=0,b=1,c=0;
       int count=2;
       while(count<=N){
           c=(a+b)%10;
           a=b;
           b=c;
           count++;
       }
       return c%10;
    }
+1
badgujarsachin835 months ago
int fib(int N){
        //code here
        int a=0,b=1,c=0;
        for(int i=0;i<N;i++){
            c=(a+b)%10;
            b=a;
            a=c;
        }
        return c;
    }
-1
geeknobita7 months ago
static int fib(int N){
        int n1=0;
        int n2=1;
        int n3=0;
        for(int i=1;i<=N;i++)
        {
            n3=(n1+n2)%10;
            n2=n1;
            n1=n3;
        }
        return n3;
    }
-1
ankitsinha1112008 months ago
0
ankitsinha111200
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.