-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigIntegers.java
More file actions
38 lines (28 loc) · 842 Bytes
/
Copy pathBigIntegers.java
File metadata and controls
38 lines (28 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.math.BigInteger;
import java.util.Scanner;
public class BigIntegers{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number : ");
int num = sc.nextInt();
BigInteger result = fib(num);
System.out.println("fib(" + num + ")" +" = "+ result);
}
public static BigInteger fib(int n ){
if(n==0){
return BigInteger.ZERO;
}
if(n==1){
return BigInteger.ONE;
}
BigInteger start = BigInteger.ZERO;
BigInteger second = BigInteger.ONE;
BigInteger next = BigInteger.ZERO;
for(int i =2; i<=n; i++){
next = start.add(second);
start = second;
second = next;
}
return next;
}
}