# GNU bc

Implementation of programming language bc

GNU bc is an implementation of bc by Free Software Foundation.

## Examples:

### Hello, World!:

Example for versions bc 1.06
``````print "Hello, World!\n";
``````

### Factorial:

Example for versions bc 1.06

This example uses recursive factorial definition.

``````define factorial(n) {
if (n == 0) return(1);
return(n * factorial(n - 1));
}

for (n = 0; n <= 16; n++) {
print n; "! = "; factorial(n);
}
``````

### Fibonacci numbers:

Example for versions bc 1.06

This example uses recursive definition of Fibonacci numbers. Note that printing “…” is put into the loop, since both code and output are shown in the same console, and printing “…” after the loop will result in the following output:

`for (n = 1; n <= 16; n++) {`
`print fibonacci(n); ", "`
`}`
`1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, print "..."`
`...`

``````define fibonacci(n) {
if (n <= 2) return(1);
return(fibonacci(n-1)+fibonacci(n-2));
}

for (n = 1; n <= 16; n++) {
print fibonacci(n); ", "
if (n==16) print "..."
}
``````

### Fibonacci numbers:

Example for versions bc 1.06

This example uses Binet’s formula. Note that bc is an arbitrary-precision calculator, so after floating-point calculations the numbers have to be rounded to integers. This has to be done manually, since bc doesn’t provide built-in rounding.

``````for (n = 1; n <= 16; n++) {
scale = 10
x = (((1 + sqrt(5)) * .5) ^ n - ((1 - sqrt(5)) * .5) ^ n) / sqrt(5)
scale = 0
print (x+0.5)/1; ", "
if (n==16) print "..."
}
``````