View Single Post
Old Aug 23rd, 2005, 11:12 PM   #1
melee28
Newbie
 
melee28's Avatar
 
Join Date: Aug 2005
Location: Orlando
Posts: 11
Rep Power: 0 melee28 is on a distinguished road
complete newbie question

Hey everyone. I recently decided to teach myself C programming by going through a college textbook and doing every single practice problem in the book. I'm still very new and prone to making lots of mistakes like this one:

I'm trying to write a program to print the first 10 digits of a Fibonacci sequence. For those that don't know, a Fibonacci sequence is one where each number is the sum of the previous two numbers. Basically, I want the program to print the following 0 1 1 2 3 5 8 13 21 34 (using mathematics, not just a blanket printf statement ). Now here's the catch... I've read a bit ahead and I know that this program could be more easily accomplished with a for loop, however, in an effort to learn things in the proper order, I've elected to do it the hard way. Here's my code so far.


#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  /*Local Definitions*/
  int fib1 = 0;
  int fib2 = 1;
  int fib3 = 1;
  int temp = 0;
  /*Statements*/
  
  printf("%2d", fib1);
  printf("%2d", fib2);
  printf("%2d", fib3);

  temp = fib1 + fib2;
  fib1 = fib2;
  fib2 = fib3;
  fib3 = temp;  
  
 
  printf("%2d", fib3);  
  
  
  printf("\n");
  system("PAUSE");	
  return 0;
}

Now this if just for the first few numbers in the sequence, but I'll add on when the program is working properly. Can anyone offer any help?

Thanks in advance.
melee28
melee28 is offline   Reply With Quote