ViijayScript: 06/26/16

Sunday, 26 June 2016

#Programs to #print #Fibonacci #series in #C #C++ #Java C# #Python

Hello netizens, On this post I am going to tell #Programs to #print #Fibonacci #series in #C #C++ #Java C# #Python.

Program in C:

#include<stdio.h>
int main ()
{
int first = 0 ,second = 1 ,third ,i ,n ;
printf ("Enter how many elements?" );
scanf ("%d" , & n );
printf ("\n%d %d" ,first ,second );
for ( i = 2 ;i < n ; ++ i )
{
third = first + second ;
printf (" %d" ,third );
first = second ;
second = third ;
}
return 0 ;
}

Program in C++:

#include<iostream.h>
#include<conio.h>
int main()
{
int a=0,b=1, c,n;
cout << "Enter the value of n ";
cin >> n;
cout << endl << a <<" " <<b << " " ;
for(int i =0; i<(n-2); i++)
{
c = a+ b;
cout << c << " ";
a=b;
b=c;
}
getch();
return 0;
}

Program in Java:

class Fibonacci {
public static void main ( String args []){
int num =Integer.parseInt ( args [0 ]) ;
System . out.println ( "Fibonacci Series" ) ;
int f1, f2 = 0 , f3 = 1;
for ( int i = 1 ;i <= num ; i ++ ){
System . out.print ( " " + f3 + " " );
f1 = f2 ;
f2 = f3 ;
f3 = f1 + f2 ;
}
}
}

Program in C#:

1. /*
2. * C#  Program to Generate Fibonacci Series
3. */
4. using System ;
5. using System.Collections.Generic ;
6. using System.Linq ;
7. using System.Text ;
8.
9. namespace fibonaci
10. {
11. class Program
12. {
13. static void Main (string [] args )
14. {
15. int i, count, f1 = 0 , f2 = 1 , f3 = 0 ;
16. Console.Write ("Enter the Limit : " );
17. count = int .Parse ( Console.ReadLine ());
18. Console.WriteLine ( f1 );
19. Console.WriteLine ( f2 );
20. for ( i = 0 ; i <= count ; i ++)
21. {
22. f3 = f1 + f2 ;
23. Console . WriteLine (f3 );
24. f1 = f2 ;
25. f2 = f3 ;
26. }
27. Console.ReadLine ();
28.
29. }
30. }
31. }

Program in Python:

def fibonacci (n):
a = 0
b = 1
for i in range(0, n):
temp = a
a = b
b = temp + b
return a

If our post is helpful then please share it with our near and dear ones.

Scan this QR Code on LinkedIn

Scan this QR Code on LinkedIn