Skip to content

Commit c4590fb

Browse files
committed
Geeko_Nacci_Number Problem
1 parent 6f46296 commit c4590fb

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @author : Piyush Kumar
3+
* Problem Link : https://practice.geeksforgeeks.org/problems/geek-onacci-number/0/?page=1&query=page1
4+
*/
5+
import java.util.*;
6+
import java.lang.*;
7+
import java.io.*;
8+
9+
class GeekoNacciNumber {
10+
public static void main (String[] args) {
11+
//code
12+
Scanner scanner = new Scanner(System.in);
13+
int t = scanner.nextInt();
14+
while(t-- > 0) {
15+
int a = scanner.nextInt();
16+
int b = scanner.nextInt();
17+
int c = scanner.nextInt();
18+
int n = scanner.nextInt();
19+
System.out.println(nthNumber(n, a, b, c));
20+
}
21+
}
22+
public static int nthNumber(int n, int a , int b, int c) {
23+
if (n == 1) {
24+
return a;
25+
}
26+
if (n == 2) {
27+
return b;
28+
}
29+
if (n == 3) {
30+
return c;
31+
}
32+
return nthNumber(n-1, a, b, c) + nthNumber(n - 2, a , b , c) + nthNumber(n-3, a, b, c);
33+
}
34+
}

0 commit comments

Comments
 (0)