Fibonacci series upto n value in C, C++, Java and Python | FACE Prep

Fibonacci series upto n value in C, C++, Java and Python | FACE Prep

Fibonacci series upto n value in C, C++, Java and Python | FACE Prep

Introduction

The Fibonacci series is a sequence where each number is the sum of the previous two numbers. It is widely used in mathematics, computer science, and nature. The first two terms are 0 and 1, and the series continues indefinitely.

Example

For n = 200, the Fibonacci series is:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144

Why Learn Fibonacci Series?

  • Found in nature, algorithms, and cryptography
  • Used in dynamic programming and recursion concepts
  • Essential for coding interviews and competitive programming

How the Fibonacci Series Works

Mathematical Formula

F(n)=F(n−1)+F(n−2)F(n) = F(n-1) + F(n-2)F(n)=F(n−1)+F(n−2)

Where:

  • F(0)=0F(0) = 0F(0)=0
  • F(1)=1F(1) = 1F(1)=1

Each subsequent number is the sum of the previous two.

Breaking It Down

  1. Start with 0 and 1.
  2. Compute the next number by adding the last two.
  3. Continue this until the value exceeds the given limit (n).

Algorithm to Generate Fibonacci Series Up to n

Steps

  1. Input the value n.
  2. Initialize three variables:
    • sum = 0, a = 0, b = 1
  3. Print the first two terms (0 and 1).
  4. Iterate while sum < n:
    • Compute sum = a + b
    • Print sum
    • Swap a = b, b = sum
  5. Stop when the value exceeds n.

Efficient Fibonacci Implementations

1 Iterative Approach (Efficient)

This method uses loops for better performance (O(n) complexity).

Python Code

def fibonacci_iterative(n):
a, b = 0, 1
print(a, b, end=" ")
while True:
next_term = a + b
if next_term > n:
break
print(next_term, end=" ")
a, b = b, next_term

# Example usage
n = int(input("Enter the limit: "))
fibonacci_iterative(n)

Optimized for large numbers
✔ Uses iteration (looping)


2 Recursive Approach (Less Efficient)

This method uses recursion, but has O(2ⁿ) complexity, making it slow for large n.

Python Code

def fibonacci_recursive(n, a=0, b=1):
if a > n:
return
print(a, end=" ")
fibonacci_recursive(n, b, a + b)

# Example usage
n = int(input("Enter the limit: "))
fibonacci_recursive(n)

Simple and elegant
Not optimized for large numbers


3 Iterative Approach Using Command-Line Arguments (C Program)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
int a = 0, b = 1, sum = 0, n;

// Convert argument to integer
n = atoi(argv[1]);

printf("%d %d ", a, b);
while (sum <= n) {
sum = a + b;
if (sum > n) break;
printf("%d ", sum);
a = b;
b = sum;
}
return 0;
}

Command-line argument support
Faster than recursion


4 Recursive Approach Using Command-Line Arguments (C Program)

#include <stdio.h>
#include <stdlib.h>

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

int main(int argc, char *argv[]) {
int n = atoi(argv[1]), sum = 0, i = 0;

while (fibonacci(i) <= n) {
printf("%d ", fibonacci(i));
i++;
}
return 0;
}

Exponential time complexity (O(2ⁿ))
Not suitable for large values of n


5 Optimized Approach Using Memoization (Dynamic Programming)

This approach stores previously computed values to improve performance.

Python Code

def fibonacci_memo(n, memo={0: 0, 1: 1}):
if n in memo:
return memo[n]
memo[n] = fibonacci_memo(n - 1, memo) + fibonacci_memo(n - 2, memo)
return memo[n]

def fibonacci_upto_n(n):
i = 0
while True:
fib = fibonacci_memo(i)
if fib > n:
break
print(fib, end=" ")
i += 1

# Example usage
n = int(input("Enter the limit: "))
fibonacci_upto_n(n)

Fast for large inputs
Avoids redundant calculations


Comparison of Methods

MethodTime ComplexitySpace ComplexityBest For
IterativeO(n)O(1)Large numbers
RecursiveO(2ⁿ)O(n)Small numbers
Dynamic Programming (Memoization)O(n)O(n)Optimized recursion
Command-line Approach (C)O(n)O(1)Efficient for compiled languages

Conclusion

The Fibonacci series is an essential concept in mathematics and programming, widely used in algorithms, problem-solving, and real-world applications. This article explored various methods to generate Fibonacci numbers up to a given value n.

Fibonacci series upto n value in C, C++, Java and Python