c - Why this code not giving desired output? -


#include <stdio.h>  //compiler version gcc 6.3.0  int main() {      float da,hra,s,gs;      scanf("%f",&s);      da=40/100*s; hra=20/100*s;      gs=s+da+hra;      printf("%f",gs);      return 0;  } 

for example if entered 25000 s output must 40000 showing 25000.000000.

try fix below.

note 40.0 , 20.0 instead of 40 , 20. issue doing integer division. 40 / 100 == 0, da 0. using 40.0 / 100 instead gives floating point division , value 0.4, want make calculations correct. (the same holds computation of hra.)

#include <stdio.h>  int main() {     float da, hra, s, gs;      scanf("%f", &s);      da = 40.0 / 100 * s;     hra = 20.0 / 100 * s;     gs = s + da + hra;      printf("%f", gs);      return 0; } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -