Prev Up Next
Go backward to 6.4 BIG project
Go up to 6 Labs
Go forward to 6.6 Robots

6.5 Fibonacci Sequence

Write a program that prints out the first 26 numbers in the Fibonacci Sequence. The sequence is named after Leonardo Fibonacci (d.c. 1250). The sequence starts with (for me) 0 and 1. (Others define the sequence as starting with the numbers 1 and 1.) Each successive number in the sequence is the sum of the previous two numbers.

Depending on the system you are using, you may get an overflow condition on the last few numbers. If so, don't worry about it, but note that it occurred on the paper that you turn in. Be sure to use integers. The Fibonacci Sequence is an integer sequence.

For the lab please print, with your program the following:

  1. Your name.
  2. CSC130 Louis Taber
  3. Lab: 6.5: Fibonacci Sequence
  4. A header for your output
  5. A sequence count and the Fibonacci sequence number (26 times). Place this part of the output on 26 separate lines.
For example:
0   1              (Starting numbers)
0 + 1 = 1          (Third number in the series)
    1 + 1 = 2      (Fourth Number in the series)
        1 + 2 = 3  (Fifth number in the series)
This program will introduce several new programming constructs as well as use the constructs that we saw in lab the lab on compiling programs. (6.1) In this lab you will deal with integer variables and constants, arithmetic (addition), and loops. You will also need a conditional expression to end the loop at the correct time and you will need to print out decimal numbers.

Integer constants are fairly easy. You have been dealing with them for years. All digits (0,1,2,3,4,5,6,7,8,9) with an optional sign in front. (In C watch out starting a number with a 0, it makes it an Octal number.) The constants that you will need in this program will be the starting numbers (0 and 1) and a number to tell the program when to stop. Please note that for integer constants that no decimal point is allowed.

Many times it is best to assign a constant to a symbolic name. If the constant changes this can now be done in only one place in the source code. There is no need to search for all places it was used.

Integer variables are referred to by symbolic names. These names are associated with the names when the variable is declared. (Even if the compiler you are using does not require that you declare all of your variables, in my class you will need to.) The variable declaration tells the compiler to set aside storage, of a specific type, to be accessed by using the symbolic name. It also will tell the compiler what type of operations are permitted and what kind of arithmetic to use. In this program (and others) the values that change need to be declared as variables. In this case you will probably need four, one for a loop counter, and three for the series.

Arithmetic expressions are also new for this program, but not the concept. For this program, the only operator needed is +. Other integer operators available will depend on the language. In Pascal the operators are +, -, *, DIV, and MOD. In "C" the operators are +, -, *, /, and %.

To assign the result of an arithmetic operation to a variable an assignment must be done. Pascal uses := in an assignment statement. "C" uses an assignment operator (=).

Loops are a way of doing the same set of instructions over and over again. Most loops will have a comparison to determine when to end/exit the loop.

For an example program I will use a different problem. I will write a program that prints out the first 20 squares and cubes. Many of the features of this program are the same as the assigned lab.

{ Louis Taber, CSC130    }
{ Lab #2 example program }
{ Pascal 9/10/97         }

program square(output);

var base_number,                { loop counter }
    number_squared, 
    number_cubed: integer;              

const max_value = 15;           { loop termination value }
      start_value = 0;          { loop beginning point }
  
begin
  writeln(' Louis Taber, CSC130, Lab #2');
  writeln(' ');
  writeln('  Number  Square    Cube');
  writeln(' ');
  for base_number := start_value to max_value do
    begin
    number_squared := base_number * base_number;
    number_cubed := base_number * number_squared; 
    writeln(base_number:8,number_squared:8,number_cubed:8); 
    end;
end.

An example of a program in "C".

/* Louis Taber, CSC130      */
/* Lab #2 example program   */ 
/* C version  9/10/97       */

#define MAX_VALUE  15      /* loop termination value */
#define START_VALUE  0     /* loop beginning point */

int main()
{
int base_number,           /* loop counter */
    number_squared, 
    number_cubed;               


  printf(" Louis Taber, CSC130, Lab #2\n\n"
         "   Number  Square    Cube\n\n");
    
  for( base_number = START_VALUE; 
       base_number <=  MAX_VALUE; base_number++ ) 
    {
    number_squared = base_number * base_number;
    number_cubed = base_number * number_squared; 
    printf(" %8d%8d%8d\n", 
            base_number, number_squared, number_cubed ); 
    }
return 0;
}
An example of a program in "FORTRAN".
!   Louis Taber, CSC130 
!   Lab #2 example program
!   FORTRAN version  9/10/97 
       PROGRAM SQUARE
!
       integer base_number           ! loop counter 
       integer number_squared 
       integer number_cubed             
!
       PARAMETER ( MAX_VALUE = 15)   ! loop ending value 
       PARAMETER ( START_VALUE =  0) ! loop beginning point 
!
       PRINT *,' Louis Taber, CSC130, Lab 2'
       PRINT *,' '
       PRINT *,'  Number  Square    Cube'
       PRINT *,' '
    
       DO 20 base_number = START_VALUE, MAX_VALUE
        number_squared = base_number * base_number
        number_cubed = base_number * number_squared 
        PRINT 40, base_number, number_squared, number_cubed
20     CONTINUE
40     FORMAT( 1X, I8, I8, I8 )  
       STOP
       END

An example of a program in "Java".

/* Louis Taber, CSC130      */
/* Lab #2 example program   */ 
/* Java version  9/10/97    */

class Square 
{
public static void main(String args[ ] )
  {
  int MAX_VALUE = 15;      /* loop termination value */
  int START_VALUE = 0;     /* loop beginning point */

  int base_number,         /* loop counter */
      number_squared, 
      number_cubed;               


  System.out.println(" Louis Taber, CSC130, Lab #2");
  System.out.println(" ");
  System.out.println("  Number  Square    Cube");
  System.out.println(" ");
         
  for( base_number = START_VALUE; 
       base_number <=  MAX_VALUE; base_number++ ) 
    {
    number_squared = base_number * base_number;
    number_cubed = base_number * number_squared; 
    System.out.println( " " + base_number +
                        " " + number_squared +
                        " " + number_cubed); 
    }
  }
}
Mark your output with: Place the lab in the instructor hand in box in BUS R6E, the "terminal room".
Instructor: ltaber@pima.edu** Red's Home page** The Pima College Site

Prev Up Next