EVEN Febonacci numbers!!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 4
Reputation: Ather14 is an unknown quantity at this point 
Solved Threads: 0
Ather14 Ather14 is offline Offline
Newbie Poster

EVEN Febonacci numbers!!

 
0
  #1
Jul 8th, 2009
I am trying generate even febonacii numbers less than four million, but I keep getting negative numbers at some point and eventually it converges to a positive answer, which is obviously wrong. Can anyone please help me out?

#include <conio.h>
#include <stdio.h>
void main (void)
{
clrscr();
int f1=1;
int f2=1;
long temp=0;
long total=0;

while (temp<4000000))
{
temp=f1+f2;
f1=f2;
f2=temp;
if (temp%2==0)
{
printf("\n %d",temp);
}
}
getch();
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: EVEN Febonacci numbers!!

 
0
  #2
Jul 8th, 2009
Major mistakes:

1>Using conio.h its an extinct header man.
2>Using clrscr() its also an extinct call.
3>Problem here is with :
  1. printf("\n %d",temp);
You are printing the long int temp as a signed integer so after it crosses the limit of 32767 it goes on to the negative side.So use use it as:
  1. printf("\n %ld",temp);
Last edited by csurfer; Jul 8th, 2009 at 5:21 am.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: Ather14 is an unknown quantity at this point 
Solved Threads: 0
Ather14 Ather14 is offline Offline
Newbie Poster

Re: EVEN Febonacci numbers!!

 
0
  #3
Jul 8th, 2009
Thanks CsurFer!!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: oop'stechie is an unknown quantity at this point 
Solved Threads: 1
oop'stechie oop'stechie is offline Offline
Newbie Poster

Re: EVEN Febonacci numbers!!

 
0
  #4
Jul 8th, 2009
u r talking about four million,thats completly out of range for an int declaration...........whether u take long int it will not work,becouse
long int don't have that much range in 32 bit processor..........
but it might work in 64 bit
u may try
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: EVEN Febonacci numbers!!

 
1
  #5
Jul 8th, 2009
I don't expect febonacci numbers to be negative, so why not use unsigned, it will give you twice the amount you can go.
And %d calls an integer, not a long (so it will only show what an integer would show if it was at that value)
I would also recomend using int main() instead of void main().
Last edited by u8sand; Jul 8th, 2009 at 1:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: EVEN Febonacci numbers!!

 
0
  #6
Jul 8th, 2009
What??! Already the 5th post and nobody has told the OP to use code tags?

To the OP:
http://www.daniweb.com/forums/announcement8-3.html
(read that information please, before making any other post, containing code snippets)
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: EVEN Febonacci numbers!!

 
0
  #7
Jul 8th, 2009
Originally Posted by oop'stechie View Post
u r talking about four million,thats completly out of range for an int declaration...........whether u take long int it will not work,becouse
long int don't have that much range in 32 bit processor..........
but it might work in 64 bit
u may try
the max of a 32 bit unsigned int is 4294967296 or 2147483648 for signed which is more than he needs. the formatting issue is the only problem.
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC