GeekInterview.com
Series: Subject: Topic:
Question: 482 of 589

Write a program to implement the Fibonacci series

This question is related to Aztec-Systems Interview
Asked by: Interview Candidate | Asked on: Jun 3rd, 2005

Editorial / Best Answer

Answered by: baseersd

View all answers by baseersd

Member Since Jun-2007 | Answered On : Jul 27th, 2007

Code
  1.  
  2. #include
  3. int main()
  4. {
  5. unsigned int i = 0, j = 0, sum = 1, num;
  6. printf("nEnter the limit for the series ");
  7. scanf("%d", &num);
  8. while (sum < num) {
  9. printf("%d ", sum);
  10. i = j;
  11. j = sum;
  12. sum = i + j;
  13. }
  14. getch();
  15. }
  16.  

Showing Answers 1 - 25 of 25 Answers
baseersd

Answered On : Jul 27th, 2007

View all answers by baseersd

Code
  1.  
  2. #include
  3. int main()
  4. {
  5. unsigned int i = 0, j = 0, sum = 1, num;
  6. printf("nEnter the limit for the series ");
  7. scanf("%d", &num);
  8. while (sum < num) {
  9. printf("%d ", sum);
  10. i = j;
  11. j = sum;
  12. sum = i + j;
  13. }
  14. getch();
  15. }
  16.  

Yes  5 Users have rated as useful.
  
Login to rate this answer.
memo132006

Answered On : Oct 8th, 2008

View all answers by memo132006

Code
  1.  
  2. #include
  3. using namespace std;
  4. void main()
  5. {
  6. int limit, sum = 0, i = 1, j;
  7. cout << "please enter the limit of the series ";
  8. cin >> limit;
  9. while (sum <= limit) {
  10. cout << sum << endl;
  11. j = sum;
  12. sum += i;
  13. i = j;
  14. }
  15. }
  16.  

Yes  1 User has rated as useful.
  
Login to rate this answer.
pullaiah06

Answered On : May 1st, 2009

View all answers by pullaiah06

Code
  1.  
  2. #include<stdio.h>
  3. #include<conio.h>
  4. {
  5. int m=0,1,size,sum,k;
  6. printf("ENTER THE SIZE OF THE series"
  7.  

Yes  1 User has rated as useful.
  
Login to rate this answer.
trogen

Answered On : May 3rd, 2009

View all answers by trogen

Code
  1.  
  2. #include
  3. #include
  4. void main()
  5. {
  6. int a, b, c;
  7. clrscr();
  8. c = a + b;
  9. a = b;
  10. b = c;
  11. printf("n %d", a);
  12. getch();
  13. }

  
Login to rate this answer.
bhaumikshah04

Answered On : Jun 19th, 2009

View all answers by bhaumikshah04

Code
  1.  
  2. #include
  3. #include
  4.  
  5. void main()
  6. {
  7. int number;
  8. clrscr();
  9. printf("Enter number till which fibonacci series");
  10. scanf("%d", &number);
  11. for (int first = 0, second = 1, temp; number >= second; temp = second, second = first + second, first = temp)
  12. printf("n%d", second);
  13. getch();
  14. }

  
Login to rate this answer.

Code
  1.  
  2. #include
  3. main()
  4. {
  5. int a, b, c, n;
  6. a = 0;
  7. b = 0;
  8. c = 1;
  9. printf("enter the value of n");
  10. scanf("%d", &n);
  11. while (n > 0) {
  12. printf("the fibanocci series is%d", c);
  13. a = b;
  14. b = c;
  15. c = a + b;
  16. n--;
  17. }
  18. }

  
Login to rate this answer.
sarabjit

Answered On : Sep 18th, 2009

View all answers by sarabjit

Code
  1.  
  2. int main()
  3. {
  4. int n, i, f1 = 0, f2 = 1, f3 = 0;
  5. printf("Enter number of terms : ");
  6. scanf("%d", &n);
  7. if (n == 1)
  8. printf("0");
  9. if (n == 2)
  10. printf("0 1");
  11. if (n >= 3) {
  12. for (i = 3; i <= n; i++) {
  13. f3 = f1 + f2;
  14. f1 = f2;
  15. f2 = f3;
  16. printf("%d", f3);
  17. }
  18. }
  19. return 0;
  20. }

  
Login to rate this answer.
shankar555

Answered On : Dec 25th, 2009

View all answers by shankar555

Code
  1.  
  2. #include<stdio.h>
  3. #include<conio.h>
  4. void main()
  5. {
  6. int i,g,h,f;
  7. clrscr();
  8. printf("nEnter the two values from which series should be started");
  9. scanf("%d%d",&g,&h);
  10. printf("nnEnter the value upto which fibonacci series should be generated");
  11. scanf("%d",&f);
  12. i=g+h;
  13. while(i<=f)
  14. {
  15. printf("n%d",i);
  16. g=h;
  17. h=i;
  18. i=g+h;
  19. }
  20. getch();
  21. }

  
Login to rate this answer.

Code
  1.  
  2. #include<stdio.h>
  3. #include<conio.h>
  4. int main()
  5. {
  6. int num,first=0,second=0,third,i;
  7. clrscr();
  8. for(i=0;i<=num;i++)
  9. {
  10. third=first+second;
  11. first=second;
  12. }
  13. printf("%d %d %d",first,second,third);
  14. getch();
  15. return 0;
  16. }

  
Login to rate this answer.
SagarDamani

Answered On : Feb 12th, 2010

View all answers by SagarDamani

Code
  1.  
  2. #include<stdio.h>
  3. #include<conio.h>
  4. void main()
  5. {
  6. int a = 1 ,b=1,i;
  7. clrscr();
  8.  
  9. printf("%d",a);
  10. printf("  %d",b);
  11.  
  12. for(i =0;i<=10;i++)
  13. {
  14. a=a+b;
  15. printf("  %d",a);
  16. b=a-b;
  17. }
  18. }
  19.  

  
Login to rate this answer.
pravigupta

Answered On : Aug 30th, 2010

View all answers by pravigupta

//fibonacci series by number of terms


Code
  1.  
  2.  
  3. #include<stdio.h>
  4. #include<conio.h>
  5.  
  6. void main()
  7. {
  8. int i=0,j=1,k,sum,term;
  9. clrscr();
  10. printf("enter the no of terms:");
  11. scanf("%d",&term);
  12. sum=i+j;
  13. printf("%d,%d,%d",i,j,sum);
  14.  
  15. for(k=1;k<=term-3;k++)
  16. {
  17. i=j;
  18. j=sum;
  19. sum=i+j;
  20. printf(",%d",sum);
  21. }
  22. getch();
  23. }
  24.  

  
Login to rate this answer.

Code
  1.  
  2. declare p number:= 0;
  3. k number: = 0;
  4. d number:= 1;
  5. c number;
  6. begin for i
  7. in 0. .100 loop c:= p + k;
  8. dbms_output.put_line(c);
  9. k:= p + d;
  10. p:= c;
  11. d:= 0;
  12. end loop;
  13. end;
  14.  

  
Login to rate this answer.
mafias18

Answered On : Oct 10th, 2010

View all answers by mafias18

Code
  1.  
  2. #include
  3. #include
  4. void main()
  5. {
  6. int n,b,a,t,i;
  7. b=0;
  8. clrscr();
  9. printf("enter the number");
  10. scanf("&d",n);
  11. t=n;
  12. while(n>0)
  13. {
  14.  a=n%10;
  15. b=b+a*a*a;
  16.  n=n/10;
  17. }
  18.  if(t==b)
  19. {
  20. printf("%d Armstrongn",t);
  21.  }
  22. else
  23.  {
  24. printf("%d not Armstrongn",t);
  25. }
  26. getch();
  27. }

  
Login to rate this answer.
elenanesg

Answered On : Oct 24th, 2010

View all answers by elenanesg

Code
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Diagnostics;
  7. namespace fibonacci {
  8. class Program {
  9. static private int MaxVal = 5;
  10.  
  11. static private int sum = 0;
  12. static private int counter = 0;
  13.  
  14. static private int fib(int n, int n2) {
  15. Trace.WriteLine(string.Format("n={0} n2={1}", n, n2));
  16.  
  17. if (counter > MaxVal)
  18. Console.Read();
  19. counter++;
  20. return fib(n2, (n + n2));
  21.  
  22. } static void Main(string[]args)
  23. {
  24. int i = fib(i, i + 1);
  25. }
  26. }
  27. }
  28.  

  
Login to rate this answer.
prititripathi

Answered On : Dec 18th, 2010

View all answers by prititripathi

Code
  1. main()
  2. {
  3. int n i,a=0.b=1,c;
  4. printf("enter d limit of series"):
  5. scanf("%d",&n);
  6. printf("%d,%d", a,b);
  7. for(i=0;i<=n-2;i++)
  8. {
  9. c=a+b;
  10. a=b;
  11. b=c;
  12. printf("%d",c);
  13. }
  14. getch();
  15. }

  
Login to rate this answer.
birlawhite

Answered On : Jan 18th, 2011

View all answers by birlawhite

Code
  1. #include<stdio.h>
  2. main()
  3. {
  4. int  n=8, i=-1, j=1;
  5. while ( n-- > 0  &&  printf ( "%d t ", j = ( i = i ) + ( i = j ) ) );
  6. }

  
Login to rate this answer.
RAHAEL

Answered On : Mar 3rd, 2011

View all answers by RAHAEL

Code
  1. program fibonacci(input,output);
  2. var :i,x,m,n:integer;
  3. begin
  4. writeln('enter the limit of series');
  5. readln(x);
  6. m:=1;
  7. n:=0;
  8. writeln(n);
  9. writeln(m);
  10. i:=0;
  11. while i<x-2 do
  12. begin
  13. i:i+1;
  14. m:m+n;
  15. n:m-n;
  16. writeln(n);
  17. if i=x-2 then
  18. writeln('finished');
  19. end;
  20. readln;end.

  
Login to rate this answer.

Code
  1. #include<stdio.h>
  2.     #include<conio.h>
  3.      
  4.     main()
  5.     {
  6.     int i, first, next, lim;
  7.     printf("Enter The Limit of Fibonacci Seriese: ");
  8.     scanf("%d", &lim);
  9.     first = 0;
  10.     next = 1;
  11.     for (i = 1; i <= lim; i++) {
  12.     printf("%d", first);
  13.     first = first + next;
  14.     next = first - next;
  15.     }
  16.     getch();
  17.     return (0);
  18.     }
  19.      

  
Login to rate this answer.
sathya

Answered On : Aug 8th, 2011

Code
  1. #include<stdio.h>
  2. #include<conio.h>
  3. main()
  4. {
  5. int n,i,c,a=0,b=1;
  6. printf("Enter fibonacci series of nth term : ");
  7. scanf("%d",&n);
  8. printf("%d %d ",a,b);
  9. for(i=0;i<=(n-3);i++)
  10. {
  11. c=a+b;
  12. a=b;
  13. b=c;
  14. printf("%d ",c);
  15. }
  16. getch();
  17. }
  18.  

  
Login to rate this answer.
MANUKUNDLOO

Answered On : Sep 8th, 2011

Code
  1. int main()
  2. {
  3.   int i=-1,j=1,sum=0,num;
  4.   printf("ENTER THE LIMIT OF THE SERIES: ");
  5.   scanf("%d",&num);
  6.   while(sum<num)
  7.     {
  8.        sum=i+j;
  9.        i=j;
  10.        j=sum;
  11.        printf("%d",sum);
  12.     }
  13.  getch();
  14. }

  
Login to rate this answer.
mahamad

Answered On : Apr 19th, 2012

Code
  1. #include<stdio.h>
  2. #include<conio.h>
  3. main()
  4. {
  5. int i,a=0,b=1;
  6. while(i<=9)
  7. {
  8. printf("%d ",a);
  9. printf("%d ",b);
  10. a=a+b;
  11. b=b+a;
  12. i++;
  13. }
  14. getch();
  15. return 0;
  16. }

  
Login to rate this answer.
jbode

Answered On : Jun 29th, 2012

View all answers by jbode

Using the computation by rounding method (see http://en.wikipedia.org/wiki/Fibonacci_number#Computation_by_rounding):

Code
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double fib_closed(unsigned long n)
  5. {
  6.   double rt5 = sqrt(5.0);
  7.   double phi = (1.0 + rt5)/2.0;
  8.  
  9.   return (floor(pow(phi,n)/rt5 + 0.5));
  10. }
  11.  
  12. int main(void)
  13. {
  14.   unsigned long i;
  15.   for (i = 0; i < 100; i++)
  16.   {
  17.     printf("fib_closed(%lu) = %.0f
  18. ", i, fib_closed(i));
  19.   }
  20.   return 0;
  21. }
  22.  

  
Login to rate this answer.
madhusudan dadhich

Answered On : Jul 24th, 2012

limit:5
01123

  
Login to rate this answer.
n.m.sudesh kumar

Answered On : Mar 20th, 2013

Code
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int pre=0,next=1,sum=0,n;
  6. clrscr();
  7. printf("enter the value of n:");
  8. scanf("%d",&n);
  9. while(pre<=n)
  10. {
  11. printf("the fibonacci series; %d
  12. ",pre);
  13. sum=pre+next;
  14. pre=next;
  15. next=sum;
  16. }
  17. getch();
  18. }

  
Login to rate this answer.
keerthi

Answered On : Jun 7th, 2013

Code for fibonacci series

Code
  1. #include<stdio.h>
  2. void main(){
  3. int i=0,j=1,sum=0,num;
  4. printf("enter the size of the series");
  5. scanf("%d",&num);
  6. while(sum<num){
  7. printf("%d",sum);
  8. i=j;
  9. j=sum;
  10. sum=i+j;
  11. }
  12. getch();
  13. }
  14.  

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.