Pages

Fibonacci Sequence Program


This program will read user input to give the number of fibonacci sequence that want to be showed. by user Then, the program will show the Fibonacci Sequence. This program is consists of 4 files. If you don’t have known about compilation using makefile, see my other posting about Make Makefile File and C Compilation using GCC.
Make inputn.c
#include "fibo.h"
#include <stdio.h>
int main()
{
    int nf;
    printf("Fibonacci Sequence Program\n");
    printf("Number of Sequence (>=1) = "); scanf("%d",&nf);
    printf("Sequence = ");
    showfibonacci(nf);
   
    return 0;
}

Make fibo.c
#include "fibo.h"
#include <stdio.h>
int fibonacci(int m)
{
    int t=0;
    if          (m == 1) {return 1;}
    else if     (m == 2) {return 1;}
    else if (m >= 3) {t = fibonacci(m-1)+fibonacci(m-2); return t;};
}

void showfibonacci(int n)
{
    int i;
    for (i=1;i<=n;i++)
    {
          if (i==1)
          {printf("%d",fibonacci(i));}
          else
          {printf(", %d",fibonacci(i));};
    }

File fibo.h
#ifndef FIBO_H
#define FIBO_H
int fibonacci(int m);
void showfibonacci(int n);
#endif


File makefile to make fiboprog.exe
fiboprog : inputn.o fibo.o
    gcc inputn.o fibo.o -o fiboprog

inputn.o : inputn.c
    gcc -c inputn.c

fibo.o : fibo.c
    gcc -c fibo.c


You can see the result below.





http://electricalgaze.com

2 comments: