0
#include <stdio.h>
int main(void)
{
    float x, y ,z,p;
    x = 22;
    y = 7;
    
    p = 22/7;
    
    z = x/y;
    
    printf("%f\n",z); //output is 3.142857
    printf("%f",p);   //output is 3.000000

    return 0;
}

`Why I had different output like this z = 3.142857 p = 3.000000

why all decimals are 0 in p and what is the different between this two methods and what is the correct method`

M.M
  • 138,810
  • 21
  • 208
  • 365
  • 6
    `22/7` is integer division. `x/y` is floating point division. Try `22.0f/7.0f` instead – UnholySheep Nov 17 '22 at 23:53
  • 355.0 / 113.0 is a much better approximation to π – Jonathan Leffler Nov 17 '22 at 23:58
  • In `x = 22`, even though `22` is an integer, since `x` is a float, there's an automatic (implicit) conversion from `int` to `float`. And then in `x/y`, you've obviously got floating-point division. But when you write `22/7`, both `22` and `7` are `int`s, and there's noting to tell the compiler you want anything else, so it sticks with integer (truncating) division. – Steve Summit Nov 18 '22 at 00:13

0 Answers0