This post explains the concept of the Fibonacci Series and their code with the help of different languages such as C, C++, Java & Python.
What is Fibonacci Series ?
In the series to find the next number the two numbers before are added and the next number is obtained. The first two numbers of the Series are 0 & 1. So, the series is 0,1,1,2,3,5,8,13………………………
There are many languages that are used to find serial numbers in programming. The Code of some languages to find the series is explained below. But before going further we need to understand some concepts used in the following code which are as follows:
For Loop:
For loop, all its loop gathered elements are gathered at one place. The general form of the loop is i.e. syntax of For Loop For C, C++, Java & Python:
For(initialization-expression ; test-expression ; update-expression)
{
Body of the Loop;
}
If Statement
If Statement is a conditional statement execute when the condition is true otherwise does not execute. Syntax of If Statement for C, C++ & Java:
If (expression)
Statement 1;
For Python syntax is
If (expression):
Statement 1;
Else if Statement
In the else-if statement, we can check more than one condition in the same code . Syntax of else If for C, C++ & Java
If (expression)
Statement 1;
else
if (expression)
Statement 2;
else
statement 3;
For Python syntax is
If (expression)
:
Statement 1;
elif (condition):
Statement 2;
else
statement 3;
Recursion:
The Function that calls itself in the execution is known as recursion. With the concept of recursion, we can have a simple and concise code that is easy to understand.
Fibonacci Program In C
Code in C:
#include<stdio.h>
int main()
{
int num1=0,num2=1,num3,i,num;
printf("Enter the number of elements:");
scanf("%d",&num);
printf("\n%d %d",num1,num2);
for(i=2;i<num;++i)
{
num3=num1+num2;
printf(" %d",num3);
num1=num2;
num2=num3;
}
return 0;
}
Output:
Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Fibonacci Series Program in C Using Recursion
Code in C using recursion:
#include<stdio.h>
void fibonacci(int num){
static int num1=0,num2=1,num3;
if(num>0){
num3 = num1 + num2;
printf("%d ",num3);
num1 = num2;
num2 = num3;
fibonacci(num-1);
}
}
int main(){
int num;
printf("Enter the number of elements: ");
scanf("%d",&num);
printf("\nFibonacci Series: ");
printf("%d %d ",0,1);
fibonacci(num-2);//n-2 because 2 numbers are already printed
return 0;
}
Output:
Enter the number of elements:15
Fibonacci Series:0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Fibonacci Series Program in C++
Code in C++:
#include <iostream>
using namespace std;
int main() {
int num1=0,num2=1,num3,i,num;
cout<<"Enter the number of elements: ";
cin>>num;
cout<<num1<<" "<<num2<<" ";
for(i=2;i<num;++i)
{
num3=num1+num2;
cout<<num3<<" ";
num1=num2;
num2=num3;
}
return 0;
}
Output:
Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Fibonacci Series Program in C++ Using Recursion
Code in C++ Using Recursion:
#include<iostream>
using namespace std;
void fibonacci(int num){
static int num1=0, num2=1, num3;
if(num>0){
num3 = num1 + num2;
cout<<num3<<" ";
num1 = num2;
num2 = num3;
fibonacci(num-1);
}
}
int main(){
int num;
cout<<"Enter the number of elements: ";
cin>>num;
cout<<"\n Fibonacci Series: ";
cout<<"0 "<<"1 ";
fibonacci(num-2);
return 0;
}
Output:
Enter the number of elements:15
Fibonacci Series:0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Fibonacci Series Program in Python
Code in Python:
num = int(input("How many terms? "))
num1, num2 = 0, 1
count = 0
if num <= 0:
print("Please enter a positive integer")
elif num == 1:
print("Fibonacci sequence upto",num,":")
print(num1)
else:
print("Fibonacci sequence:")
while count < num:
print(num1)
num3 = num1 + num2
num1 = num2
num2 = num3
count += 1
Output:
How many terms 5
Fibonacci sequence:
0
1
1
2
3
Fibonacci Series Program in Java
Code in Java:
import java.util.Scanner;
class FibonacciSeries{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("enter the number of elements:");
int num = sc.nextInt();
int num1=0,num2=1,num3,i;
System.out.print(num1+" "+num2);
for(i=2;i<num;++i)
{
num3=num1+num2;
System.out.print(" "+num3);
num1=num2;
num2=num3;
}
}}
Output:
enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Fibonacci Using Recursion in Java
Code using recursion in Java:
import java.util.Scanner;
class FibonacciSeries{
static int num1=0,num2=1,num3=0;
static void Fibonacci(int num){
if(num>0){
num3 = num1 + num2;
System.out.print(" "+num3);
num1 = num2;
num2 = num3;
Fibonacci(num-1);
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("enter the number of elements:");
int num = sc.nextInt();
System.out.print(num1+" "+num2);
Fibonacci(num-2);
}
}
Output:
enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Discover more from easytechnotes
Subscribe to get the latest posts sent to your email.
you r doing a great job
plz upload something about kotlin
Is kotlin overtakes java?
thanku dear for suggesting. Shortly you will get the post on Kotlin