943,633 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 657
  • C++ RSS
Jul 8th, 2009
0

EVEN Febonacci numbers!!

Expand Post »
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();
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ather14 is offline Offline
4 posts
since Jul 2009
Jul 8th, 2009
0

Re: EVEN Febonacci numbers!!

Major mistakes:

1>Using conio.h its an extinct header man.
2>Using clrscr() its also an extinct call.
3>Problem here is with :
c++ Syntax (Toggle Plain Text)
  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:
c++ Syntax (Toggle Plain Text)
  1. printf("\n %ld",temp);
Last edited by csurfer; Jul 8th, 2009 at 5:21 am.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jul 8th, 2009
0

Re: EVEN Febonacci numbers!!

Thanks CsurFer!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ather14 is offline Offline
4 posts
since Jul 2009
Jul 8th, 2009
0

Re: EVEN Febonacci numbers!!

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
oop'stechie is offline Offline
4 posts
since Jul 2009
Jul 8th, 2009
1

Re: EVEN Febonacci numbers!!

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.
Reputation Points: 78
Solved Threads: 15
Junior Poster
u8sand is offline Offline
131 posts
since Dec 2008
Jul 8th, 2009
0

Re: EVEN Febonacci numbers!!

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)
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 8th, 2009
0

Re: EVEN Febonacci numbers!!

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.
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with .exe
Next Thread in C++ Forum Timeline: A few user input questions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC