|
|
|
| Web Hosting Guide |
![]() ![]() |
Fibonacci Number Program Problem, n00b :) |
Jan 22 2006, 10:25 AM
Post
#1
|
|
|
Way Out Of Control - You need a life :) Group: [HOSTED] Posts: 1,267 Joined: 2-August 05 From: Kapellen (Antwerp, Belgium) Member No.: 7,585 myCENTs:4.00 |
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 I've mainly used this information to make the program. Ow, and any tips to improve my code are always welcome |
|
|
|
Jan 22 2006, 12:06 PM
Post
#2
|
|
|
Way Out Of Control - You need a life :) Group: [HOSTED] Posts: 1,267 Joined: 2-August 05 From: Kapellen (Antwerp, Belgium) Member No.: 7,585 myCENTs:4.00 |
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 |
|
|
|
Feb 13 2006, 07:36 PM
Post
#3
|
|
|
Cosmic Overlord Group: Members Posts: 571 Joined: 26-November 05 From: Denver, Colorado, US Member No.: 9,811 myCENTs:45.66 |
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. |
|
|
|
Feb 14 2006, 10:13 AM
Post
#4
|
|
|
Veteran Nut Group: Members Posts: 527 Joined: 4-October 05 From: UK Member No.: 8,895 |
You could always comment your coding throughout, that way it is easier to refer to it later on.
|
|
|
|
May 28 2006, 12:46 PM
Post
#5
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 23 Joined: 20-August 05 Member No.: 7,964 |
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) [snapback]69786[/snapback] 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. |
|
|
|
Jun 18 2006, 09:03 PM
Post
#6
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 28 Joined: 13-August 05 Member No.: 7,827 |
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.
|
|
|
|
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
7 | tansqrx | 1,921 | 30th October 2009 - 12:39 PM Last post by: iG-gurjeet singh |
|||
![]() |
10 | yordan | 2,184 | 27th October 2009 - 11:31 PM Last post by: HannahI |
|||
![]() |
68 | BooZker | 35,334 | 25th October 2009 - 03:33 PM Last post by: iG-Jon |
|||
![]() |
9 | Logan Deathbringer | 1,798 | 13th October 2009 - 06:56 PM Last post by: iG-Doulos |
|||
![]() |
16 | marcin | 3,365 | 9th October 2009 - 08:16 PM Last post by: iG-Earl J. Photomiester |
|||
![]() |
3 | Eggie | 1,253 | 2nd October 2009 - 12:45 AM Last post by: iG-candy |
|||
![]() |
16 | Chesso | 1,701 | 28th September 2009 - 10:59 PM Last post by: iG-Carol |
|||
![]() |
9 | mpinsky | 2,898 | 21st September 2009 - 07:15 AM Last post by: iGuest |
|||
![]() |
5 | al421552 | 802 | 18th September 2009 - 10:44 AM Last post by: iG-djay4me |
|||
![]() |
10 | takerraj | 155 | 17th September 2009 - 04:25 AM Last post by: xboxrulz |
|||
![]() |
16 | victorhu | 6,401 | 13th September 2009 - 10:02 PM Last post by: iG-Happy Unlocker User |
|||
![]() |
5 | surfermac | 211 | 11th September 2009 - 09:06 PM Last post by: Curt200518 |
|||
![]() |
22 | Ronel | 5,369 | 10th September 2009 - 12:25 AM Last post by: iG-Icetea |
|||
![]() |
9 | doudou | 3,392 | 6th September 2009 - 08:30 PM Last post by: iGuest |
|||
![]() |
17 | CrazyPensil | 5,239 | 20th August 2009 - 02:49 AM Last post by: iG-Sheen |
|||
|
Lo-Fi Version | Time is now: 8th November 2009 - 06:36 PM |
© 2009 AstaHost: Free Web Hosting & Technical Discussion, Free Web Hosting. a member of xisto.
Powered by Invision Board. Skin: IPB Forum Skins
Expand / Collapse Navigation



Jan 22 2006, 10:25 AM





