Fibonacci Number Program Problem - n00b :)

free web hosting
Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

Fibonacci Number Program Problem - n00b :)

wutske
I (finaly) started learning C. I'm using a book to learn it combined with some trial and error.
Now I've made a program to calculate the Fibonacci numbers, but there seems to be some error.

This is the source code:
CODE

#include  <stdio.h>

main()
{
int   fib1, fib2, fib, i, inp;

printf("************************************************");
printf("\n* Welcome to the Fibonacci Number Program v0.1 *\n");
printf("************************************************");
printf("\nWich Fibonacci Number do you want?\n");
scanf("%d", &inp);
fib1 = 1;
fib2 = 0;
switch(inp)
 {
 case 0:
   printf("\n Sorry, no result for f(0)");
   break;
 case 1:
   printf("\n Fib(1) = 0");
   break;
 case 2:
   printf("\n Fib(1) = 0");
   printf("\n Fib(2) = 1");
   break;
 default:
   printf("\n Fib(1) = 0");
   printf("\n Fib(2) = 1");
   for (i=1; i<= inp -2; ++i)
    {
    fib = fib1 + fib2;
    fib2 = fib1;
    fib1 = fib;
    printf("\n Fib(%d) = %d", i + 2, fib);
    }
 }
}


and this is the output for 55:

CODE

************************************************
* Welcome to the Fibonacci Number Program v0.1 *
************************************************
Wich Fibonacci Number do you want?

Fib(1) = 0
Fib(2) = 1
Fib(3) = 1
Fib(4) = 2
Fib(5) = 3
Fib(6) = 5
Fib(7) = 8
Fib(8) = 13
Fib(9) = 21
Fib(10) = 34
Fib(11) = 55
Fib(12) = 89
Fib(13) = 144
Fib(14) = 233
Fib(15) = 377
Fib(16) = 610
Fib(17) = 987
Fib(18) = 1597
Fib(19) = 2584
Fib(20) = 4181
Fib(21) = 6765
Fib(22) = 10946
Fib(23) = 17711
Fib(24) = 28657
Fib(25) = 46368
Fib(26) = 75025
Fib(27) = 121393
Fib(28) = 196418
Fib(29) = 317811
Fib(30) = 514229
Fib(31) = 832040
Fib(32) = 1346269
Fib(33) = 2178309
Fib(34) = 3524578
Fib(35) = 5702887
Fib(36) = 9227465
Fib(37) = 14930352
Fib(38) = 24157817
Fib(39) = 39088169
Fib(40) = 63245986
Fib(41) = 102334155
Fib(42) = 165580141
Fib(43) = 267914296
Fib(44) = 433494437
Fib(45) = 701408733
Fib(46) = 1134903170
Fib(47) = 1836311903
Fib(48) = -1323752223
Fib(49) = 512559680
Fib(50) = -811192543
Fib(51) = -298632863
Fib(52) = -1109825406
Fib(53) = -1408458269
Fib(54) = 1776683621
Fib(55) = 368225352


So something goes wrong at Fib(47), but I don't know what mellow.gif
I've mainly used this information to make the program.

Ow, and any tips to improve my code are always welcome wink.gif

 

 

 


Reply

wutske
I suddenly realised that an int is limited in size, so I changed the fibs to float and the problem is solved.
New code:
CODE

#include  <stdio.h>

main()
{
int   i, inp;
float fib1, fib2, fib;

printf("************************************************");
printf("\n* Welcome to the Fibonacci Number Program v0.1 *\n");
printf("************************************************");
printf("\nWich Fibonacci Number do you want?\n");
scanf("%d", &inp);
fib1 = 1;
fib2 = 0;
switch(inp)
 {
 case 0:
   printf("\n Sorry, no result for f(0)");
   break;
 case 1:
   printf("\n Fib(1) = 0");
   break;
 case 2:
   printf("\n Fib(1) = 0");
   printf("\n Fib(2) = 1");
   break;
 default:
   printf("\n Fib(1) = 0");
   printf("\n Fib(2) = 1");
   for (i=3; i<= inp; ++i)
    {
    fib = fib1 + fib2;
    fib2 = fib1;
    fib1 = fib;
    printf("\n Fib(%d) = %.0f", i, fib);
    }
 }
}


The new output is now:
CODE

************************************************
* Welcome to the Fibonacci Number Program v0.1 *
************************************************
Wich Fibonacci Number do you want?

Fib(1) = 0
Fib(2) = 1
Fib(3) = 1
Fib(4) = 2
Fib(5) = 3
Fib(6) = 5
Fib(7) = 8
Fib(8) = 13
Fib(9) = 21
Fib(10) = 34
Fib(11) = 55
Fib(12) = 89
Fib(13) = 144
Fib(14) = 233
Fib(15) = 377
Fib(16) = 610
Fib(17) = 987
Fib(18) = 1597
Fib(19) = 2584
Fib(20) = 4181
Fib(21) = 6765
Fib(22) = 10946
Fib(23) = 17711
Fib(24) = 28657
Fib(25) = 46368
Fib(26) = 75025
Fib(27) = 121393
Fib(28) = 196418
Fib(29) = 317811
Fib(30) = 514229
Fib(31) = 832040
Fib(32) = 1346269
Fib(33) = 2178309
Fib(34) = 3524578
Fib(35) = 5702887
Fib(36) = 9227465
Fib(37) = 14930352
Fib(38) = 24157816
Fib(39) = 39088168
Fib(40) = 63245984
Fib(41) = 102334152
Fib(42) = 165580128
Fib(43) = 267914272
Fib(44) = 433494400
Fib(45) = 701408640
Fib(46) = 1134903040
Fib(47) = 1836311680
Fib(48) = 2971214848
Fib(49) = 4807526400
Fib(50) = 7778741248
Fib(51) = 12586267648
Fib(52) = 20365008896
Fib(53) = 32951275520
Fib(54) = 53316284416
Fib(55) = 86267559936

 

 

 


Reply

Vyoma
I am not sure about it, but float may result in rounding off errors (May not pertain to this particular example).

Try using 'long' and see if you get the same result.

Reply

twitch
You could always comment your coding throughout, that way it is easier to refer to it later on.

Reply

Unitechy
Well firstly i'm considering fibonacii series starting with 1 1

there are two types starting with 0 and starting with 1 i am sure you know this.

Secondly why 're you taking switch its tedious..

This is simple and easy to understand

CODE

#include <stdio.h>
void main()
{
  int a=1,b=1,c,x;
    printf("Enter the number for which fibonacii to be found");
    scanf("%d",&n);
    printf("%d \n %d \n,a,b);
    for(x=1;x<=n-2;x++);
    { c=a+b;
     printf("%d\n",c);
     a=b;
     b=c;
    }
}


i hope this helped.
Btw don't shout on me if it didn't cause even i am new learner lol

QUOTE(Vyoma @ Feb 13 2006, 08:36 AM) *

I am not sure about it, but float may result in rounding off errors (May not pertain to this particular example).

Try using 'long' and see if you get the same result.


Reply

abhishek
Try using sizeof() function to determine the size of int and long. This will be different in different systems. Sometimes the size of long will be equal to that of int and sometimes small will be equal to int. The last number is 5bytes long so the size of long should be greater than 5 if you will use long.

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. fibonacci series program in c - 6.99 hr back. (2)
  2. c program for fibonacci series - 25.20 hr back. (1)
  3. fibonacci numbers in c programming - 25.49 hr back. (1)
  4. fibonocci c program - 25.62 hr back. (1)
  5. sum of n fibonacci numbers c program - 27.62 hr back. (1)
  6. fibonacci program - 4.87 hr back. (3)
  7. fibonacci c - 41.69 hr back. (1)
  8. c programs to calculate the fibonacci series - 41.82 hr back. (1)
  9. example of fibonancci program - 54.73 hr back. (1)
  10. a program that will determine the fibonacci of a number - 67.75 hr back. (1)
  11. fibonacci series program - 5.73 hr back. (5)
  12. fibonacci number with c programming - 75.64 hr back. (1)
  13. fibonacci number program - 77.49 hr back. (1)
  14. fibonacci numbers programming codes in c - 77.63 hr back. (1)
Similar Topics

Keywords : fibonacci, number, program, problem, n00b

  1. C++ Meters To Feet And Feet To Meters Program
    I made it (0)
  2. My First C++ Program: Area, Volume Etc. Calculator
    My first program! (5)
    CODE //This program calculates the perimeter, area, surface area, or volume of any shape or
    solid. #include <iostream> #include <string> #define PI 3.14159265359 using
    namespace std; float psquare (float a) { return (4*a); } float prectangle
    (float w, float l) { return ((2*w)+(2*l)); } float pparallelogram
    (float b, float h) { return ((2*b)+(2*h)); } float ptrapezoid
    (float s, float t, float b) { return ((2*s)+b+t); } float pcircle
    (float....
  3. Math Program
    arithmetical progression problem (2)
    Short program in c to calculate first term when sum of n terms and common difference d is given.....
  4. Vc Runtime Lib Error When Run A Program
    (3)
    the program was tested in my machine with VS.net installed. It run without any problem with 24
    hours. However, when I run it in other machine without VS.NET or VC ienvironment, it didn't
    works. Even I installed dot.net framework and copy MFC, VC's dll files to this PC, it just don;t
    run. The following are the dll files that I copied: mfc40.dll mfc40loc.dll mfc40u.dll mfc42.dll
    MFC42CHS.DLL mfc42loc.dll mfc42u.dll MFC71.dll MFC71CHS.DLL MFC71CHT.DLL mfc71d.dll MFC71DEU.DLL
    MFC71ENU.DLL MFC71ESP.DLL 2005-04-01 01:02 61,440 MFC71FRA.DLL 2005-04-01 01:02 61,440 MFC7....

    1. Looking for fibonacci, number, program, problem, n00b

Searching Video's for fibonacci, number, program, problem, n00b
advertisement




Fibonacci Number Program Problem - n00b :)



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE