floating point - C - Print int variable in float -
after looking answers still coudnt find solution code.
variables needs type int, in 1 case need print decimal solution. have tried casting, still didint work.
#include<stdio.h> int main() { int sum = 6; int product = 24; int differenz, result, division, rest, decimal; differenz = ++product - sum++; result = differenz * sum; division = result / 9; rest = result % 9; decimal = result / 9.0; printf("result = %d\n", result); printf("integer division = %d\n", division); printf("remainder = %d\n", rest); printf("division = %.2f\n", (float) decimal); return 0; }
output:
result = 133 integer division = 14 remainder = 7 division = 14.00 <-- here supposed print 14.76
i have tried:
float b = (float) decimal; printf("division = %.2f\n", b);
still division = 14.00
any tip appreciated. in advance!
this because created variable decimal int , you're trying print float.
either create variable float, or print int, cannot mix both.
also, casting int float add ".00", won't able modify that.
Comments
Post a Comment