0

I know this has to be something extremely idiotic, but I really can't find my mistake. I'm trying to write a program to calculate (2/i)^i, where i is an integer put in input by the user. If i = 0, the function returns 1. This is the function I have been using.

double f (int x)
{
    double y = 1.0;
    if (x == 0)
    return 1;
    else
    for (int i = 1; i <= x; i++)
    y *= pow (2/i, i);
    return y;
}

int main ()
{
    for(int i=0;i<=10;++i)
    cout << "f(" << i << ") =" << f(i) << endl;
}

Now.. This is supposed to be an extremely simple code. Yet, it doesn't work. It compiles correctly, but the results are not what I'm expecting. After some iterations, I always get 0 as a result. What am I doing wrong?

1 Answers1

1

Integer division:

change to:

y *= pow (2.0/i, i);
          ^^^

Also, why do you need a loop?

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541