+ 2
Why both of them have different output ?
1ļøā£ int main() { int a, b, c, sum; float avg; a=1; b=6; c=3; sum=a+b+c; avg=sum/3; cout<<"Sum = "<<sum<<endl; cout<<"Average = "<<avg; return 0; } 2ļøā£ int main() { int a, b, c; float sum, avg; a=1; b=6; c=3; sum=a+b+c; avg=sum/3; cout<<"Sum = "<<sum<<endl; cout<<"Average = "<<avg; return 0; } Why the 1st code prints the integral value of Average and the 2nd one prints the floating value āā
5 Answers
+ 4
int/int results int
double/int => double
int/double => double
double/double => double
Values are upcasted to higher types in calculations and result is casted back to destination types..
In first, sum is int type so int/int return int data only..
While second sum is of float data type so float/int returns float data type value... Hope it helps..
+ 3
Jayakrishnaš®š³ Ok got it....
Thanks š
+ 3
AĶ¢J Got it... Thanks
+ 1
Jainil Raval š®š³
sum variable is declared as float so
sum = 1 + 6 + 3 = 10.0
And 10.0 / 3 gives float value
+ 1
You're welcomeJainil Raval š®š³