• 0

[C] Need someone to code this


Question

I am not a math wiz, but I need someone to write a program in C that would print out this formula:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55 .......

The formula is the last 2 numbers in the list are added to make the next number.

1 + 1 = 2

1 + 2 = 3

2 + 3 = 5

...

I don't really know how I can do this myself. I understand what I need to do with the loops and arrays, but I can't get it to work at all.

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

I think the code pretty much speaks for itself...

#include <stdio.h>

#define START	1
#define END  100

int main(void)
{
    int a, b, c;

    a = b = START;

    printf("%li", b);

    while(b < END)
    {
        c = a;
        b = a+b;
        a = b;
        b = c;
  
        printf(", %li", b);
    }

    return 0;
}

And by the way.. a google search for "fibonacci series" is probably going to give you more help if you need it.

Edited by hdood
Link to comment
Share on other sites

  • 0

So, pray tell, what are you going to use a Fibonacci Sequence for inside this Python script of yours? Fib Sequences are classic first year CS...

Here's a recursive method for generating it:

int fib(int n)
{
    return (n < 2) ? 1 : (fib(n - 1) + fib(n - 2));
}

Link to comment
Share on other sites

  • 0

Acually, here is the exact code I needed:

#!C:/Python/python.exe

START = 1
END = 16384
a = START
b = START
i = 1
while b < END:
     c = a
     b = a + b
     a = b
     b = c
     print i, b, "=", hex(b)
     i = i + 1

Edited by b0b
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.