-->

Facebook

Jasper Roberts - Blog

Monday, November 3, 2014

Write a c program to print first N Fibonacci Numbers.

#include<conio.h>
#include<stdio.h>

void main()
{
      int num1, num2, num3, n;
      int count;
     
      clrscr();
     
      num1 = 0;
      num2 = 1;

      printf("How Many Fibonacci Numbers?");
      scanf("%d",&n);

      if(n>2)
      {
            printf("\n The Fibonacci Numbers are:");
            printf("%5d%5d",num1,num2);
            count = 2;

            while(count<n)
            {
                  num3 = num1 + num2;
                  printf("%5d",num3);
                  num1 = num2;
                  num2 = num3;
                  count++;
            }
      }
      else
      {
            printf("\n 0    1");
      }
      getch();
}

OUTPUT:
How Many Fibonacci Numbers? 7
The Fibonacci Numbers are: 0  1  1  2  3  5  8

No comments:

Post a Comment