C Programming – Fibonacci Sequence


The first program written in C language (Except the Hello World Program) – For showing Fibonacci Sequence.

Still, the program is far from perfect and has its upper limit (To the 46th term, by using long integer type).

#include <stdio.h>
#define max_term 46
int chdigit(int zero_one);
void Fib(long term);
main()
 {
     printf("This program lists the Fibonacci sequence.\n");
     Fib(max_term);
 }
int chdigit(int zero_one)
 {
if (zero_one == 1)
return(0);
else
         return(1);
 }
void Fib(long term)
 {
 long i, F[3]; //By using an integral array of 3 elements
     F[2] = 0;
     F[1] = F[0] = 1;
     printf("F(0) = %15d\n", F[2]);
     printf("F(1) = %15d\n", F[1]);
     printf("F(2) = %15d\n", F[0]);
 for (i = 3; i < (term + 1); ++i)
     {
         F[i % 2] += F[chdigit(i % 2)];
         printf("F(%d) = %15d\n", i, F[i % 2]);
     }
 }

When I was writing the program, I tried using i%1 to change the value of i from 0 to 1 or 1 to 0, but certainly failed, as whenever i == 0, i (operator) (operand) == 0.

Nonetheless, that’s just a good try.

One thought on “C Programming – Fibonacci Sequence

塗牆

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s