Program to print Fibonacci series.

Here, we have a basic program example to print the Fibonacci series using different languages. Fibonacci is a series of numbers in which each number is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc. This program is created in c language, c++, Java, and Python

Code to print Fibonacci series in C language

#include <stdio.h>
int main() {

  int i, n;
  int t1 = 0, t2 = 1;
  int nextTerm = t1 + t2;
  printf("Enter the number of terms: ");
  scanf("%d", &n);
  printf("Fibonacci Series: %d, %d, ", t1, t2);
  for (i = 3; i <= n; ++i) {
    printf("%d, ", nextTerm);
    t1 = t2;
    t2 = nextTerm;
    nextTerm = t1 + t2;
  }

  return 0;
}

Code to print Fibonacci series in C++ language

#include<iostream>
using namespace std;
int main()
{
  int i, n;
  int t1 = 0, t2 = 1;
  int nextTerm = t1 + t2;
  cout<<"Enter the number of terms: ";
  cin>>n;
  cout<<"Fibonacci Series: " << t1 <<" " << t2;
  for (i = 3; i <= n; ++i) {
    cout<<" "<<nextTerm<<" ";
    t1 = t2;
    t2 = nextTerm;
    nextTerm = t1 + t2;
  }
 return 0;
}

Code to print Fibonacci series in Python language

n = int(input("Enter the number of terms:  "))
n1, n2 = 0, 1
count = 0
if n <= 0:
   print("Please enter a positive integer")
elif n == 1:
   print("Fibonacci sequence upto",n,":")
   print(n1)
else:
   print("Fibonacci sequence:")
   while count < n:
       print(n1)
       nth = n1 + n2
       n1 = n2
       n2 = nth
       count += 1

Code to print Fibonacci series in Java language

import java.util.*;
public class fibo {

    public static void main(String[] args) {
 
      int i, n;
	  int t1 = 0, t2 = 1;
	  int nextTerm = t1 + t2;
	System.out.println("Enter the number of terms:  ");
        Scanner s=new Scanner(System.in);
        n = s.nextInt();
	System.out.println("Fibonacci Series: \n"+ t1 + "\n" + t2);
	   for (i = 3; i <= n; ++i) {
	    System.out.println(nextTerm);
	    t1 = t2;
	    t2 = nextTerm;
	    nextTerm = t1 + t2;
	  }     
    }
}