Skip to content

Commit

Permalink
Merge pull request #121 from Billakanti-Teja/feature/fibonacci
Browse files Browse the repository at this point in the history
Add fibonacci program in general coding questions package
  • Loading branch information
abhishektripathi66 authored Feb 21, 2025
2 parents 496bc83 + 871c75d commit a4d4598
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Coding Questions/General questions/NthFibonacci.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package GeneralQuestions;

import java.util.Scanner;

//Program to find out the Nth number in the fibonacci series
public class NthFibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n < 0) {
System.out.println("Invalid input! N must be a non-negative integer.");
} else {
System.out.println(fibo(n));
}

sc.close();
}

static int fibo(int n) {
if(n==0||n==1)return n;
int first = 0;
int second = 1;
int current = 0;
for (int i = 2; i <= n; i++) {
current = first + second;
first = second;
second = current;
}
return current;

}
}

0 comments on commit a4d4598

Please sign in to comment.