Sunday, March 13, 2011

C program to print fibonacci numbers

Program
#include <stdio.h>

int main()
{
 int a,b,temp,n;

 printf("\nEnter the number upto which you want fibonacci sequence: ");
 scanf("%d",&n);
 
 printf("\nFibonacci Sequence upto %d:\n",n);

 a=0;
 b=1;
 printf("\n%d, %d",a,b);
 while (b <= n)
 {
  temp=b;
  b=a+b;
  a=temp;
  if (b <= n) printf(", %d",b);
 }
 printf("\n\n");

}
Compilation, Run and Output
[sreedhar@manchu2 cprogs_work_machine]$ gcc fibonacci.c -o fibonacci
[sreedhar@manchu2 cprogs_work_machine]$ ./fibonacci 

Enter the number upto which you want fibonacci sequence: 21

Fibonacci Sequence upto 21:

0, 1, 1, 2, 3, 5, 8, 13, 21

[sreedhar@manchu2 cprogs_work_machine]$

No comments:

PBS Script Generator: Interdependent dropdown/select menus in Javascript

PBS SCRIPT GENERATOR
SH/BASH TCSH/CSH
Begin End Abort

About Me

LA, CA, United States
Here I write about the battles that have been going on in my mind. It's pretty much a scribble.

Sreedhar Manchu

Sreedhar Manchu
Higher Education: Not a simple life anymore