Fibonacci series in c | c++ and java

If you are searching for Fibonacci series in c | c++ and java then you are at the right article.

Note in this article simple iterative approach is used.

Table of contents

Topics
Definition of fibonacci series.
Example fibonacci sequence.
Prerequisite programming requirements for Fibonacci series in c | c++ and java.
Fibonacci series in c.
Fibonacci series in c++.
Fibonacci series in java.
Further programming concepts.

What is fibonacci series?

Fibonacci series is a series in which each number in this sequence is sum of two preceeding numbers in fibonacci series.


Example fibonacci sequence

1, 1, 2, 3, 5,8,13…


Prerequisite programming requirements for Fibonacci series in c | c++ and java.

  • Input output operation in c | c++ and java.
  • Unary addition operator in c | c++ and java.
  • Assignment operator in c | c++ and java.
  • Data-types in c | c++ and java.

Fibonacci series in c.

#include<stdio.h>
int main(int argc, char const *argv[])
{
	int number;
	printf("\nFibonacci sequence in c\nEnter a number upto print fibonacci series.\n");
	scanf("%d",&number);
	int term1,term2;
	//First two series of fibonacci series is 0 and 1
	term1=0;
	term2=1;
	int next_term;
	printf( "\n%d, %d",term1,term2);
	for( int i=0 ; i<number ; i++ )
	{
		next_term = term1+term2;
		printf(", %d",next_term);
		term1=term2;
		term2=next_term;
	}


	return 0;
}
Fibonacci series in c
Fibonacci series in c

Fibonacci series in c++

#include<iostream>
using namespace std;
int main(int argc, char const *argv[])
{
	int number;
	cout<<"\nFibonacci sequence in c++\nEnter a number upto print fibonacci series.\n";
	cin>>number;
	int term1,term2;
	//First two series of fibonacci series is 0 and 1
	term1=0;
	term2=1;
	int next_term;
	cout<<"\n"<<term1<<", "<<term2;
	cout<<term1<<", "<<term2;
	for( int i=0 ; i<number ; i++ )
	{
		next_term = term1+term2;
		cout<<" ,"<<next_term;
		term1=term2;
		term2=next_term;
	}


	return 0;
}
Fibonacci series in c++
Fibonacci series in c++

Fibonacci series in java

import java.util.Scanner;

public class FibonacciSeriesInJava {

	public static void main(String[] args) {
		int number;
		Scanner stdin=new Scanner(System.in);
		int term1,term2;
		term1=0;
		term2=1;
		System.out.println("Fibonacci series in java\n Enter a number to print that number of series elements");
		number=stdin.nextInt();
		int nextTerm=term1+term2;
		System.out.print(term1+", "+term2);
		for( int i=0 ; i<number ; i++ )
		{
		  nextTerm=term1+term2;
		  System.out.print(", "+nextTerm);
		  term1=term2;
		  term2=nextTerm;	  
		}
		
		
	}

}
Fibonacci series in java
Fibonacci series in java

Further practice reading