How to name recursion in different ways?

When I run this code, I have an exception like

Unhandled exception in 0x779C017E (ntdll.dll) in ConsoleApplication1.exe: 0x00000000: operation completed successfully.

If there is a handler for this exception, the program can safely continue.

How did I fix it? And thanks in advance.

#include<stdio.h>

int fn(int n) {
if (n == 1)
  return 1;
  return fn(n - 1) + fn(n - 2);
}

int main()
{
 int n, k = 1;
 scanf_s("%d",&n);
 k = fn(n);
 printf("%d", k);
 return 1;

}
+4
source share
3 answers

There is a condition in your code that will always lead to an infinite loop (not just for 3).

5 . 5 4 3. 3 2 1. 2 1 0. , 0, .

, , , 0 . .

if (n <= 1)
   return 1;
+4

, n 3. , , n < 3 return 1

+2

#include<stdio.h>

   int fn(int n) {
   if (n == 1 || n == 0)
      return 1;
      return fn(n - 1) + fn(n - 2);
   }

int main()
{
  int n, k = 1;
  scanf("%d",&n);
  k = fn(n);
  printf("%d", k);
  return 1;

}

    if (n == 1 || n == 0)
+1

All Articles