-1
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
    int x,i;
    double abc,sum=0;
    printf("Enter a value for x:");
    scanf("%d",&x);
    for(i=0;i<=6;i++)
    {
        abc=pow(1/2,i)*pow((x-1)/x,i+1);
        sum=sum+abc;
    }
    printf("Sum is %f\n",sum);

}

As what i have checked there is no overflow of values in data type either or is it something else?

drac_o
  • 427
  • 5
  • 11

1 Answers1

0

The issue is in this line of code:

abc=pow(1/2, i) * pow((x-1) / x, i + 1);

1/2 is always zero, and (x - 1)/x is also zero when x > 0. You can use 0.5 or 1.0 / 2.0 if you'd like to use a decimal value. Also, be careful about dividing by zero.

The resulting code would look like this:

abc=pow(0.5, i) * pow((x - 1.0)/x, i + 1.0);
R.F. Nelson
  • 2,254
  • 2
  • 12
  • 24
  • that was so quick, I got it. So how do I make sure about these small negligence every time I code something, will it get better by practicing more or I would have to take special attention while writing every bit? – drac_o May 28 '18 at 13:44
  • @drac_o You’ll get better at preventing these types of errors as you practice more. The more you code, the more you’ll be able to recognize these issues. If this answer helped you, please mark it as accepted. Thanks, and best of luck! – R.F. Nelson May 28 '18 at 15:21
  • thanks for the suggestion – drac_o May 28 '18 at 17:42