Saturday, February 18, 2012

Algorithm (Lab report 02)

Theory: Have you ever looked closely at a pinecone or a sunflower? Did you know that there are mathematical patterns in nature and in man-made objects as well?
A 13th Century mathematician, Leonardo of Pisa, nicknamed "Fibonacci", discovered a number pattern that was named after him, called the Fibonacci Sequence. You will be investigating the Fibonacci Sequence and patterns using the web resources below.
Apparatus:
           i.     Computer.
           ii.    Coding Language.
          iii.    Compiler (Code block, Dev c/c++, Turbo c/c++ etc.)
          iv.    Coding skill.
Algorithm: ( Fibonacci number generator) Iterative .
Step 01:  function fib(n : integer) return integer is
Step 01:   first  : integer := 0;
Step 01:   second : integer := 1;
Step 01:   tmp    : integer;
Step 01:   begin
Step 01:   for i in 1..n loop
Step 01:      tmp    := first + second;
Step 01:      first  := second;
Step 01:      second := tmp;
Step 01:   end loop;
Step 01:   return first;
Step 01:  end fib;

Source code: (Fibonacci number generator)
# include<stdio.h>
# include<conio.h>
void fibonacci(long);
int main()
{
clrscr();
long N;
scanf("%ld",&N);
fibonacci(N);
getch();
return 0;
}
void fibonacci(long N)
          {
          long i,f0=0,f1=1,f;
          for(i=0;i<N;i++)
                   {
                   f=f0+f1;
                   f1=f0;
                   f0=f;
                   printf("%3d",f1);
                   }
          }



OR
# include<stdio.h>
# include<conio.h>
int main()
{
clrscr();
long N, i,f0=0,f1=1,f;
scanf("%ld",&N);
for(i=0;i<N;i++)
                        {
                        f=f0+f1;
                        f1=f0;
                        f0=f;
                        printf("%3d",f1);
                        }
getch();
return 0;
}

Sample Input                                           Sample output

5                                                                0  1  1  2  3
8                                                                0  1  1  2  3  5  8  13
10                                                              0  1  1  2  3  5  8  13  21  34
3                                                                0  1  1

Caution: You must change this code. And also change the test value when you print it......Try to make change or modify this.