GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Nov-2004, 15:30
bluedragon27 bluedragon27 is offline
New Member
 
Join Date: Nov 2004
Posts: 1
bluedragon27 is on a distinguished road

Help with a simple program.


i have to write a program that shows the first 24 numbers of the Fibonacci sequence of numbers. The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21,... or the next number is the sum of the previous two numbers. I have a program that works but instead of starting with 0 it starts with 1. Here is the code, any help would be greatly appreciated.
CPP / C++ / C Code:
#include<stdio.h>

int main()
{

int m = 0;
int k = 1;
int i;


{
printf("Stephen Gill sugill");
printf("\n\n");
}

for (i=0; i<25; i++)
{
int a= m + k;
m = k;
k = a;


printf("\n%d",a);
printf("\n");
}
return(m);
}
  #2  
Old 16-Nov-2004, 16:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by bluedragon27
i have to write a program that shows the first 24 numbers of the Fibonacci sequence of numbers. The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21,... or the next number is the sum of the previous two numbers. I have a program that works but instead of starting with 0 it starts with 1. Here is the code, any help would be greatly appreciated.
CPP / C++ / C Code:
#include<stdio.h>

int main()
{

int m = 0;
int k = 1;

Well, you used m and k for the first two terms of your sequence. You didn't print them out. So you could simply do this:
CPP / C++ / C Code:
/* your stuff */

int m = 0;
int k = 1;
int i;
{  /* why did yout put this in a separate block? doesn't actually hurt */
   /* but is not common C style   */
printf("Stephen Gill sugill");
printf("\n\n");
}

printf("%d\n\n", m); /* or you could just printf("0\\n\\n"); */
printf("%d\n", k);   /* or you could just printf("1\\n");   */

 /* the loop and the rest of your stuff here */


Regards,

Dave
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 14:30
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 21:25
Need help with a C program (Long) McFury C Programming Language 3 29-Apr-2004 21:06
How to write simple program like ........? laputa9000 C++ Forum 3 29-Oct-2003 13:09

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:26.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.