0

I wrote a program in which I found the area of cylinder using a functionarea with a return-type and without parameters. answerwas returned to main function. However, I am getting different output in main and a different output in area. The decimal places seem to be replaced by 0 in the main function. Why is it so? enter image description here

Shail
  • 881
  • 9
  • 20
  • 37

3 Answers3

2

Change the return type of area from int to float

Ben
  • 221
  • 1
  • 7
1

Your function returns an int which truncates any real part of the value.

float area()
luser droog
  • 18,988
  • 3
  • 53
  • 105
1

Your variable answer is float in area function but return type of area function is int. so it typed cased during return, in main

int area(){
 ^  should be float

 float answer;

 // print float
 return answer;
}

int main(){
 answer = area(); // answer gets integral part
 // print int part
}
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208