| | |
EVEN Febonacci numbers!!
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 4
Reputation:
Solved Threads: 0
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();
}
#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();
}
Major mistakes:
1>Using conio.h its an extinct header man.
2>Using clrscr() its also an extinct call.
3>Problem here is with :
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>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)
printf("\n %d",temp);
c++ Syntax (Toggle Plain Text)
printf("\n %ld",temp);
Last edited by csurfer; Jul 8th, 2009 at 5:21 am.
I Surf in "C"....
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().
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.
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)
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."
![]() |
Similar Threads
- Loop counting odd and even numbers (C++)
- C++ Random Numbers (C++)
- storing large numbers (C++)
- Add two numbers (C++)
- random numbers all different??? (C++)
- Overlocking Pointless? (Motherboards, CPUs and RAM)
Other Threads in the C++ Forum
- Previous Thread: Help with .exe
- Next Thread: A few user input questions
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






