Nth Fibonacci Number

 

Nth term of Fibonacci series F(n), where F(n) is a function, is calculated using the following formula -

    F(n) = F(n-1) + F(n-2), 
    Where, F(1) = F(2) = 1

Provided N you have to find out the Nth Fibonacci Number.

Sample Input 1:

6

Sample Output 1:

8
import java.util.Scanner;
public class Solution {


	public static void main(String[] args) {
	
        Scanner sc = new Scanner(System.in);
     	int n = sc.nextInt();
        int c=0;
        int a =0;
        int b= 1;
        
        if(n==1 || n==2)
        {
            System.out.println(1);

        }
        else
        {
			for(int i=2;i<=n;i++)
            {
				c=a+b;
                a=b;
                b=c;
            }
            System.out.println(c);

        }
        

		
	}

}

Comments

Popular posts from this blog

Minimum Length Word

Check Number Sequence

Star Pattern