Jump to content
xisto Community
Sign in to follow this  
wutske

Fibonacci Number Program Problem n00b :)

Recommended Posts

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:

#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:

************************************************* 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 :D
I've mainly used this information to make the program.

Ow, and any tips to improve my code are always welcome :D

Share this post


Link to post
Share on other sites

I suddenly realised that an int is limited in size, so I changed the fibs to float and the problem is solved.
New 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:
************************************************* 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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

#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

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.


Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
hi..how can i know the answer for thisFibonacci Number Program Problem

machine problem:

fibonacci series:

1. Enter 1st number :5

enter 2nd number :2

enter number of iteration: 5

output: 5 2 7 9 16

-reply by gigi

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.