Program to print Fibonacci series in Python | C | C++ | Java

Problem statement:- Program to print Fibonacci series

 Data requirement:-

   Input Data:- n

  Output Data:-c

  Additional Data:-a,b,i

Program in C
  
Here is the source code of the C Program to print the Fibonacci series.

Code:

#include<stdio.h>
int main()
{
    int n,i=1,a=0,b=1;
    printf("Enter the range of number(Limit):");
    scanf("%d",&n);
    int c=a+b;
    while(i<=n)
    {
     printf("%d ",c);
     c=a+b;
     a=b;
     b=c;
     i++;
    }
}

Input/Output:
Enter the range of number(Limit):5
1 1 2 3 5

Program in C++
  
Here is the source code of the C++ Program to print the Fibonacci series.

Code:

#include<iostream>
using namespace std;
int main()
{
    int n,i=1,a=0,b=1;
    cout<<"Enter the range of number(Limit):";
    cin>>n;
    int c=a+b;
    while(i<=n)
    {
     cout<<c<<" ";
     c=a+b;
     a=b;
     b=c;
     i++;
    }
}

Input/Output:
Enter the range of number(Limit):7
1 1 2 3 5 8 13

Program in Java
  
Here is the source code of the Java Program to print the Fibonacci series.

Code:

import java.util.Scanner;
public class p13 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in); 
int n,i=1,a=0,b=1;
    System.out.println("Enter the range of number(Limit):");
    n=cs.nextInt();
    int c=a+b;
    while(i<=n)
    {
     System.out.print(c+" ");
     c=a+b;
     a=b;
     b=c;
     i++;
     }
     cs.close();
}
}

Input/Output:
Enter the range of number(Limit):
9
1 1 2 3 5 8 13 21 34 

Program in Python
  
Here is the source code of the Python Program to print the Fibonacci series.

Code:

print("Enter the range of number(Limit):")
n=int(input())
i=1
a=0
b=1
c=a+b
while(i<=n):
    print(c,end=" ")
    c = a + b
    a = b
    b = c
    i+=1

Input/Output:
Enter the range of number(Limit):
7
1 1 2 3 5 8 13 


Post a Comment

0 Comments