Hi there sorry for this noob question. I'm Mario and may I ask if my program is correct for recursive and non-recursive function for Fibonacci Secquence nth Value.
static int recursiveMethod(int num)
{
if (num <= 1)
return num;
return recursiveMethod(num-1) + recursiveMethod(num-2);
}
static int nonRecursiveMethod(int num) {
if (num == 0) {
return 0;
}
if (num == 1) {
return 1;
}
int first = 0;
int second = 1;
int nth = 1;
for (int i = 2; i <= num; i++) {
nth = first + second;
first = second;
second = nth;
}
return nth;
}
For summary: Example I inputted 6 as my nth value. Then the outputs are RECURSIVE: 8 then NON-RECURSIVE: 1 1 2 3 5 8
Is that correct?