Sunday, July 26, 2009

C++ program 3?

write a c++ program to display a series of 15 terms of fibonacci series...DON'T GIVE ANY LINKS PLEASE....reply immediately if you can programme it....need for an exam...be quick...

C++ program 3?
I have a program here but coded in c, you can change it easily because they're almost the same like instead of using stdio.h change it into iostream.h, cout%26lt;%26lt; for printf and cin%26gt;%26gt; for scanf.





Here's the code:





#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





void main()


{


int num=0, num2=1, tot;


int des, i;


clrscr();


printf("Enter the desired fibonacci number: ");


scanf("%d",%26amp;des);





printf(" %d \n %d",num,num2);


for(i=2;i%26lt;des;i++)


{


tot = num+num2;


num=num2;


num2=tot;


printf("\n %d",tot);


}


getch();


}





This code will ask you to input the desired fibonacci number but the problem here is you are asking us for the fixed one which is 15...





Here's the code:





#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





void main()


{


int num=0, num2=1, tot;


int i;


clrscr();





printf("Series of 15 numbers in fibonacci are:\n");


printf(" %d \n %d",num,num2);


for(i=2;i%26lt;15;i++)


{


tot = num+num2;


num=num2;


num2=tot;


printf("\n %d",tot);


}


getch();


}





I don't remember if when converting source code of c into c++, clrscr(); and getch(); functions are not included.


Try to experiment but these are the logic of what you are looking.


Hope these help. ^^
Reply:You're being lazy.





create a variable to store the fibinooci result


loop thru x amount of times


calculate the fibinocci number


store result


display result


repeat.


No comments:

Post a Comment