Write a program to implement the Fibonacci series

This question is related to Aztec-Systems Interview

Editorial / Best Answer

baseersd  

  • Member Since Jun-2007 | 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 - 75 of 75 Answers

memo132006

  • Oct 8th, 2008
 

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.  

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. }

  Was this answer useful?  Yes

sarabjit

  • Sep 18th, 2009
 

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. }

  Was this answer useful?  Yes

shankar555

  • Dec 25th, 2009
 

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. }

  Was this answer useful?  Yes

pravigupta

  • Aug 30th, 2010
 

//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.  

  Was this answer useful?  Yes

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.  

  Was this answer useful?  Yes

mafias18

  • Oct 10th, 2010
 

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. }

  Was this answer useful?  Yes

elenanesg

  • Oct 24th, 2010
 

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.  

  Was this answer useful?  Yes

RAHAEL

  • Mar 3rd, 2011
 

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.

  Was this answer useful?  Yes

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.      

  Was this answer useful?  Yes

sathya

  • 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.  



  Was this answer useful?  Yes

MANUKUNDLOO

  • 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. }

  Was this answer useful?  Yes

mahamad

  • 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. }

  Was this answer useful?  Yes

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.  

  Was this answer useful?  Yes

madhusudan dadhich

  • Jul 24th, 2012
 

limit:5
01123

  Was this answer useful?  Yes

n.m.sudesh kumar

  • 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. }

  Was this answer useful?  Yes

keerthi

  • 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.  

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions