0

Why is the value of x not changing

How do you use a fraction with an integer in c

#include <stdio.h>

int main(void)
{
    int x , y , n=0;
    scanf("%d", &x);
    scanf("%d", &y);
    printf("%d %d\n",x , y);
        
    x = (13/12) * x;
    printf("%d\n", x );

}

katysha
  • 51
  • 2
  • 7
  • 1
    `13/12` is an *integer division* and its value is 1. Try `x = (13.0/12) * x;`. Note that `x` is also an integer value and the result will be truncated. Try with large enough value like `x = 1000`. – MikeCAT Jul 22 '21 at 12:52
  • 1
    Another way (avoiding floating-point calculation) is `x = 13 * x / 12;` – MikeCAT Jul 22 '21 at 12:52
  • @MikeCAT: Doing `13*x/12` will not round properly. Example: `13 * 7 / 12`, the exact answer is `7.58`, which should properly round to `8`. But your formula will truncate to `7`. – abelenky Jul 22 '21 at 13:12
  • @abelenky It is unclear that the result should be truncated or rounded. – MikeCAT Jul 22 '21 at 13:13

0 Answers0