0

Getting the answer as 4+3+1.50000=8 instead of 8.5

# include< stdio.h> Int main() { int a =4; int b=3; float c= 1.5; int sum = a+b+c; printf ("the sum is %d+%d+%f=%d",a,b,c,sum); }

30th Jul 2022, 9:14 PM
Kushal Sharma
Kushal Sharma - avatar
4 odpowiedzi
+ 4
Because it's int sum but it needs to be Double/ float
30th Jul 2022, 10:16 PM
Felix Alcor
Felix Alcor - avatar
+ 4
You can specify the type before you assign the variable: Also you can simply print sum as float. #include <stdio.h> int main() { int a,b; float c,sum; a = 4; b = 3; c = 1.5; sum = a + b + c; printf ("the sum is %f",sum); return 0; }
30th Jul 2022, 10:52 PM
Chris Coder
Chris Coder - avatar
+ 2
# include<stdio.h> int main() { int a =4; int b=3; float c= 1.5; float sum = a+b+c; printf ("the sum is %d+%d+%f=%f",a,b,c,sum); } Hope this will help u... U have to use float sum instead of int sum for printing decimal no. And also in printf u should use %f as format specifier for float data types.
1st Aug 2022, 8:58 AM
Shruti Bubna
Shruti Bubna - avatar
0
Thank you alot guys .. I got the problem
31st Jul 2022, 11:36 AM
Kushal Sharma
Kushal Sharma - avatar