Division in c not giving expected value

丶灬走出姿态 提交于 2019-12-06 21:03:27

Because you are doing an integer division, try with:

div = 25.0/8;

or

div = (double)25/8;

Typing 25.0 means a double literal. You could also use 25.f for a float literal. Both of these trigger floating point division.

Either typecast explicitly to double data type or change numerator to get desired value i.e. 25.o or use floating point literal 25.f.

brokenfoot

Typecast it, i.e. change it to:

double div;
div = (double)25/(double)8;
printf("%lf",div);

Since both the numerator and denominator are integer values, division operator performs only integer division and skips fractional part for optimization purpose.

So you need to specify by typecasting or making 25 as 25.0 or 8 as 8.0 that you want fractional part as well.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!