Try the fix below.
Note the 40.0
and 20.0
instead of 40
and 20
. The issue is that you were doing integer division. 40 / 100 == 0
, so da
was always 0
. Using 40.0 / 100
instead gives you floating point division and the value 0.4
, which is what you want to make your calculations correct. (The same holds for the 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;
}
solved Why this code not giving desired output?