C PROGRAMMING PROGRAM • LOOPS & NUMBER PROBLEMS
Print the Fibonacci Series
Learn how to print the fibonacci series using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
8
Sample output
0 1 1 2 3 5 8 13
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
int terms;
unsigned long long first = 0, second = 1;
printf("Enter number of terms: ");
scanf("%d", &terms);
if (terms < 1 || terms > 94) {
printf("Use a term count from 1 to 94.\n");
return 0;
}
for (int index = 0; index < terms; index++) {
printf("%llu%c", first, index == terms - 1 ? '\n' : ' ');
unsigned long long next = first + second;
first = second;
second = next;
}
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
0 1 1 2 3 5 8 13
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Print the current term, then shift the pair forward using their sum.
- Display the computed result.
Print the current term, then shift the pair forward using their sum.
EFFICIENCY
Time and space complexity
Time complexity
O(n)
Auxiliary space
O(1)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
