write c code for fibonacci
- User
write c code for fibonacci
Assistant
Here's a C program to calculate the nth number in the Fibonacci series:
#include <stdio.h> int fibonacci(int n) { if (n <= 1) { return n; } return fibonacci(n-1) + fibonacci(n-2); } int main() { int n; printf("Enter the number for which the Fibonacci series needs to be calculated: "); scanf("%d", &n); int result = fibonacci(n); printf("The %dth number in the Fibonacci series is: %d\n", n, result); return 0; }Explanation:
- First, we define a function
fibonacciwhich takes an integernas input. - We check if
nis less than or equal to 1, if so, we return as it's the 1st or 0th number in the Fibonacci series.
- First, we define a function
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.