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

Program to generate Fibonacci series upto n value is discussed here. Fibonacci series is a series in which each number is the sum of the last two preceding numbers. The first two terms of a Fibonacci series are 0 and 1.

For example, generate fibonacci series upto 200.

The first two terms are 0 and 1.

  • 0 + 1 = 1
  • 1 + 1 = 2
  • 1 + 2 = 3
  • 2 + 3 = 5
  • 3 + 5 = 8
  • 5 + 8 = 13
  • 8 + 13 = 21
  • 13 + 21 = 34
  • 21 + 34 = 55
  • 34 + 55 = 89
  • 55 + 89 = 144
  • 144 + 89 = 233

The fibonacci series upto 200 is given as follows.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144.

Algorithm to generate Fibonacci series upto n value

  1. Input the n value until which the Fibonacci series has to be generated.
  2. Initialize sum = 0,  a = 0 and b = 1
  3. Print the first two terms of the series, a and b.
  4. sum = a + b
  5. If(sum < n)
  6. print (sum)
  7. swap a and b and swap b and sum.
  8. If (sum > n)
  9. End the algorithm.
  10. Else 
  11. Repeat from steps 4 to 7.

Program to generate Fibonacci series upto n value using Iterative method

// C program to generate fibonacci series upto n value

#include<stdio.h>
int main()
{
int sum = 0, n;
int a = 0;
int b = 1;
printf("Enter the nth value: ");
scanf("%d", &n);
printf("Fibonacci series: ");
while(sum <= n)
{
printf("%d ", sum);
a = b; // swap elements
b = sum;
sum = a + b; // next term is the sum of the last two terms
}
return 0;
}
// C++ program to generate fibonacci series upto n value

#include<iostream>
using namespace std;
int main()
{
int sum = 0, n;
int a = 0;
int b = 1;
cout << "Enter the nth value: ";
cin >> n;
cout << "Fibonacci series: ";
while(sum <= n)
{
cout << sum << " ";
a = b;  // swap elements
b = sum;
sum = a + b;  // next term is the sum of the last two terms
}
return 0;
}
// Java program to generate fibonacci series upto n value

import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int sum = 0, n;
int a = 0;
int b = 1;
System.out.println("Enter the nth value: ");
n = sc.nextInt();
System.out.println("Fibonacci series: ");
while(sum <= n)
{
System.out.print(sum + " ");
a = b;  // swap elements
b = sum;
sum = a + b;  // next term is the sum of the last two terms
}
}
}
# Python program to generate Fibonacci series upto n value

n = int(input("Enter the nth value: "))
a = 0
b = 1
sum = 0
print("Fibonacci Series : ", end = " ")
while(sum <= n):
     print(sum, end = " ")
     a = b
     b = sum
     sum = a + b
#include<stdio.h>
int main(int argc, char *argv[])
{
int sum = 0, n;
int a = 0;
int b = 1;
n = atoi(argv[1]);
while(sum <= n)
{
printf("%d ", sum);
a = b; // swap elements
b = sum;
sum = a + b; // next term is the sum of the last two terms
}
return 0;
}

Program to generate Fibonacci series upto n value using Recursive method

#include<stdio.h>
int Fibonacci(int n)
{
if (n <= 1)
return n;
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
int main()
{
int n, sum = 0;
printf("Enter the nth value: ");
scanf("%d", &n);
printf("Fibonacci series: ");
while(Fibonacci(sum) <= n)
{
printf("%d ", Fibonacci(sum));
sum++;
}
}
#include<iostream>
using namespace std;
int Fibonacci(int n)
{
if (n <= 1)
return n;
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
int main()
{
int n, sum = 0;
cout << "Enter the nth value: ";
cin >> n;
cout << "Fibonacci series: ";
while(Fibonacci(sum) <= n)
{
cout << Fibonacci(sum) << " ";
sum++;
}
}
import java.util.Scanner;
public class Main
{
public static int Fibonacci(int n)
{
if (n <= 1)
return n;
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n, sum = 0;
System.out.println("Enter the nth value: ");
n = sc.nextInt();
System.out.println("Fibonacci series: ");
while(Fibonacci(sum) <= n)
{
System.out.print(Fibonacci(sum) + " ");
sum++;
}
}
}
#include<stdio.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, sum = 0;
n = atoi(argv[1]);
while(Fibonacci(sum) <= n)
{
printf("%d ", Fibonacci(sum));
sum++;
}
}

Output:

fibonacci series upto n value

Time complexity: O(n)