Object

Write a program to print the fibonacci series up till a specified range.

Algorithm

  1. Declare integer variables num and i and long integer variables a, b and c.
  2. a = 0, b = 1, c = 1
  3. Read num.
  4. Repeat FOR i = 1 to num by 1
    • Write a.
    • c = a + b.
    • a = b.
    • b = c.
    • [End of FOR loop.]
  5. Exit.

Flowchart

C++ Source Code

// program 39
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  int i, num;
  long int a = 0, b = 1, c = 1;

  cout << "Enter the number to generate Fibonacci series: ";
  cin >> num;

  for (i = 1; i <= num; i++)
  {
    cout << setw(12) << a;
    c = a + b;
    a = b;
    b = c;
  }
  return 0;
}

C Source Code

/*program 39*/
#include<stdio.h>

int main()
{
  int i, num;
  long int a=0, b=1, c=1;
  printf("Enter the number to generate fibonacci series: ");
  scanf("%d", &num);
  for(i=1; i<=num; i++)
     {
     printf("%8ld\t", a);
     c = a + b;
     a = b;
     b = c;
     }
  return 0;
}

Output

Enter the number to generate Fibonacci series: 40
           0           1           1           2           3           5           8          13
          21          34          55          89         144         233         377         610
         987        1597        2584        4181        6765       10946       17711       28657
       46368       75025      121393      196418      317811      514229      832040     1346269
     2178309     3524578     5702887     9227465    14930352    24157817    39088169    63245986
Design a site like this with WordPress.com
Get started