0
#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?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Arpit Tomar
  • 187
  • 1
  • 8

2 Answers2

2

Because a is an int. You should cast a to a float, like this

b = (float)a /4;
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Liyuan Liu
  • 172
  • 2
1

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