# Problem with recursive function for an expression

• 11-28-2012
sanzor
Problem with recursive function for an expression
Hey everyone i need help with this function that displays the result of the following expression : expr=1/1+2/2+3/3+5/4+8/5+...
where the numerator represents a fibonaci number.
Code:

```#include<stdio.h> int fibo(int n) {     if(n==0 || n==1)     return 1;     return fibo(n-1)+fibo(n-2); } float expr(int n) {     if(n==1)     return 1;     return (fibo(n)/(n))+expr(n-1); } int main() {     int n,m=1;     printf("dami n");     scanf("%d",&n);     printf("fibonaci de %d=%d\n",n,fibo(n));     printf("expresie de %d=%f",n,expr(n)); }```
• 11-28-2012
Nominal Animal
Code:

```sanzor.c: In function ‘main’: sanzor.c:22:11: warning: unused variable ‘m’ [-Wunused-variable] sanzor.c:24:10: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result] sanzor.c:29:1: warning: control reaches end of non-void function [-Wreturn-type]```
Are you aware that float type has just seven significant digits or precision? You might wish to use double for this kind of thing.

Edit 1: Fibonacci(0) = 0, Fibonacci(1) = 1, Fibonacci(2) = 1. Your fibo() function has wrong seed values (Fibonacci(1) = 1, Fibonacci(2) = 1).

Edit 2: If you have two integer variables or expressions, / does an integer division. Therefore, on line 14, your code does an integer division, discarding any fractional part of the result. To calculate the division using floating-point values, cast one or both to float or double.

In other words, if you have integer variables or expressions a and b, then a/b is also an integer: the fractional decimals have been discarded.

If you want the result as a double, then use a/(double)b for example. That casts b to double type, thus promoting the result to double type too.
• 12-07-2012
sanzor
Thank you i thought that if the function returns a float result it is enough...