#include<stdio.h>
void main(){
int a;float b;
scanf("%d",&a);
b=a/4;
printf("%f",b);
}
For example, 9 divided by 4 should print 2.25 but instead it prints 2.0000. Why?
#include<stdio.h>
void main(){
int a;float b;
scanf("%d",&a);
b=a/4;
printf("%f",b);
}
For example, 9 divided by 4 should print 2.25 but instead it prints 2.0000. Why?
Because a
is an int
. You should cast a
to a float
, like this
b = (float)a /4;
a
is an integer, so your division is performed with integers throughout then the result is coerced to a float when you assign it to b