Fibonacci series program

Fibonacci series in c

#include<stdio.h>
#include<conio.h>
void main()
{
	int a=0,b=1,s=0,n;
	printf("Enter the number of elements \n");
	scanf("%d",&n);
	printf("%d %d ",a,b);
	for(int i=2;i<n;i++)
	{
		s=a+b;
		printf("%d ",s);
		a=b;
		b=s;
	}
	getch();
}

Output

Enter the number of elements
7
0 1 1 2 3 5 8

Fibonacci series in c++

#include<iostream>
#include<conio.h>
using namespace std;
class FibonacciSeries
{
public:
	void show(int n)
	{
		int a=0,b=1,s=0;
		cout<<a<<" "<<b<<" ";
		for(int i=2;i<n;i++)
		{
			s=a+b;
			cout<<s<<" ";
			a=b;
			b=s;
		}
	}
};
void main()
{
	int n;
	FibonacciSeries f;
	printf("Enter the number of elements \n");
	scanf("%d",&n);
	f.show(n);
	getch();
}

Output

Enter the number of elements
8
0 1 1 2 3 5 8 13

Fibonacci series in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FibonacciSeriesProject
{
    class FibonacciSeries
    {
       public void show(int n)
       {
            int a = 0, b = 1, s = 0;
            Console.Write(a + " " + b + " ");
            for (int i = 2; i < n; i++)
            {
                s = a + b;
                Console.Write(s + " ");
                a = b;
                b = s;
            }
        }
        static void Main(string[] args)
        {
            FibonacciSeries fs = new FibonacciSeries();
            Console.WriteLine("Enter the number of elements");
            int input = Convert.ToInt32(Console.ReadLine());
            fs.show(input);
            Console.ReadLine();
        }
    }
}

Output

Enter the number of elements
12
0 1 1 2 3 5 8 13 21 34 55 89

Fibonacci series in java

import java.util.*;
class FibonacciSeries
{
	public void show(int n)
    {
         int a = 0, b = 1, s = 0;
         System.out.print(a + " " + b + " ");
         for (int i = 2; i < n; i++)
         {
             s = a + b;
             System.out.print(s + " ");
             a = b;
             b = s;
         }
     }
}
class examples
{
public static void main(String[] args)
	{
	 FibonacciSeries fs=new FibonacciSeries();
	 Scanner sc=new Scanner(System.in);
	 System.out.println("Enter the number of elements");
	 int input=sc.nextInt();
	 fs.show(input);
    }
}

Output

Enter the number of elements
13
0 1 1 2 3 5 8 13 21 34 55 89 144 

Write a comment


Name Email Message