0
Make a division in C
Make a division of two int, in a programme of calculator in C. My result is always 0. What should I do ? Thanks. https://www.sololearn.com/fr/compiler-playground/c98x4xoDVtOj
4 Answers
+ 1
Hello Charbel,
Can you provide some input examples to test the code with? I would like to try a closer look on it to see what went wrong.
0
If your division result is always 0, it may be because you are performing integer division. In C, when you divide two integers, the result will also be an integer, so any decimal part will be truncated. To fix this issue, you can cast one or both of the integers to a floating-point type (like float or double) before performing the division. This way, you'll get a floating-point result. In this example below, I've cast a to a float before performing the division to ensure that the result is a floating-point number ⤾ď¸
https://sololearn.com/compiler-playground/cD4wtDrLu4EZ/?ref=app
0
Try 10/2*3+5
0
Charbel
#include <stdio.h>
int main() {
int a = 10;
float result = (float)a /2*3+5; // Cast one operand to float
printf("Result: %.2f\n", result);
return 0;
}
Try this!!