# Program to find Fibonacci series up to n

17 May 2023

Write a program to find Fibonacci series up to n ## Description about Program to find Fibonacci series up to n

Fibonacci series is a special series where nth term is the sum of previous two terms in the series. The series starts with 0 and 1 as the first and second term of the series respectively.

Here you nee to get the value for nth term from user and then print Fibonacci series containing n terms.

Input

5

Output

0,1,1,2,3

Input

8

Output

0,1,1,2,3,5,8,13

## C Program to find Fibonacci series up to n

### Method 1

#include <stdio.h>

int main()

{

int num, a=-1,b=1,c;

printf("Enter a number: ");

scanf("%d",&num);

printf("Fibonacci series: ");

for(int i=0;i<num;i++)

{

c=a+b;

printf("%d, ",c);

a=b;

b=c;

}

return 0;

}

### Method 2

#include <stdio.h>

int fibonacci(int num)

{

static int a = 0, b = 1, c;

if(num > 0)

{

c = a + b;

a = b;

b = c;

printf("%d, ",c);

fibonacci(num-1);

}

}

int main()

{

int num;

printf("Enter the number");

scanf("%d",&num);

printf("0, 1, ");

fibonacci(num-2);

return 0;

}

## C++ Program to find Fibonacci series up to n

### Method 1

#include <iostream>

using namespace std;

int main()

{

int num, a=-1,b=1,c;

cout<<"Enter a number: ";

cin>>num;

cout<<"Fibonacci series: ";

for(int i=0;i<num;i++)

{

c=a+b;

cout<<c<<",";

a=b;

b=c;

}

return 0;

}

### Method 2

#include <iostream>

using namespace std;

int fibonacci(int num)

{

static int a = 0, b = 1, c;

if(num > 0)

{

c = a + b;

a = b;

b = c;

cout<<c<<", ";

fibonacci(num-1);

}

return 0;

}

int main()

{

int num;

cout<<"Enter the number: ";

cin>>num;

cout<<"0, 1, ";

fibonacci(num-2);

return 0;

}

## Java Program to find Fibonacci series up to n

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number : ");

int num = sc.nextInt();

if(num > 0)

{

int a = 0, b = 1, c;

System.out.print("Fibonacci Series : "+a+", "+b+", ");

while(b < num)

{

c=a+b;

a=b;

b=c;

if(b <= num)

System.out.print(b+", ");

}

}

else

System.out.print("Invalid Input");

}

}

## Python Program to find Fibonacci series up to n

num = int(input("Enter the Number:"))

a, b = 0, 1

print("Fibonacci Series:", a,",", b, end=" , ")

for i in range(2, num):

c = a + b

a = b

b = c

print(c,",", end=" ")

If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023 