write c code for fibonacci
- User
write c code for fibonacci
Assistant
Here is a C code to generate the Fibonacci sequence:
c#include <stdio.h> int main() { int n, i = 0, t1 = 0, t2 = 1, nextTerm; printf("Enter the number of terms: "); scanf("%d", &n); printf("Fibonacci Series: "); for (i = 1; i <= n; ++i) { printf("%d, ", t1); nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; } return 0; }This code takes the number of terms n as input from the user, initializes the first two Fibonacci numbers t1 and t2 as 0 and 1 respectively, prints the first term, and then enters a loop to calculate and print the subsequent terms by adding the previous two terms. The loop runs n times to print the Fibonacci series up to the nth term.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.