Search code examples
cfloating-pointfloating-point-conversion

C - how to divide floats?


I get input from command line as a int d. Now I am facing this problem:

float a,b;
int d;
float piece;    
printf("Please enter the parts to divide the interval: ");
scanf("%d", &d);

a=0;
b=1;

piece=b-a/(float)d;
printf("%f\n",piece);

All I want is to printf some float number dependent on &d. e.g. when I write here 5, I would get 0.20000, for 6 - 0,166666 but I am still getting 1.000000 for all numbers, does anyone knows solution?


Solution

  • Use parenthesis:

    piece=(b-a)/(float)d;