Large Fibonacci Numbers in Java Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 6 Likes Like Report Given a number n, find n-th Fibonacci Number. Note that n may be large. Examples: Input : 100 Output : 354224848179261915075 Input : 500 Output : 139423224561697880139724382870 407283950070256587697307264108962948325571622 863290691557658876222521294125 Prerequisite: BigInteger Class in Java, Fibonacci numbersFibonacci of large number may contain more than 100 digits, it can be easily handled by BigInteger in Java. BigInteger class is used for the mathematical operation which involves very big integer calculations that are outside the limit of all available primitive data types. JAVA // Java program to compute n-th Fibonacci // number where n may be large. import java.io.*; import java.util.*; import java.math.*; public class Fibonacci { // Returns n-th Fibonacci number static BigInteger fib(int n) { BigInteger a = BigInteger.valueOf(0); BigInteger b = BigInteger.valueOf(1); BigInteger c = BigInteger.valueOf(1); for (int j=2 ; j<=n ; j++) { c = a.add(b); a = b; b = c; } return (b); } public static void main(String[] args) { int n = 100; System.out.println("Fibonacci of " + n + "th term" + " " +"is" +" " + fib(n)); } } OutputFibonacci of 100th term is 354224848179261915075 Note that the above solution takes O(n) time, we can find the n-th Fibonacci number in O(log n) time. As an exercise, find the n-th Fibonacci number for large n in O(log n) time.This article is contriBigInteger.valueOf (1);but by Pramod Kumar. See your article appearing on the GeeksforGeeks' main page and help other Geeks. Create Quiz Comment K kartik Follow 6 Improve K kartik Follow 6 Improve Article Tags : Java Fibonacci large-numbers Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like