+ 1

how to add integer part of the decimal numbers by c language

https://prnt.sc/1x85esy https://prnt.sc/1x85gmq https://prnt.sc/1x85i04 https://prnt.sc/1x85j4p and this is the code I have done https://code.sololearn.com/cP2VMloNnJ94 Can anyone help me with saying where I went wrong

24th Oct 2021, 10:04 AM
morgan
morgan - avatar
10 Réponses
+ 3
Your code seem to be correct i guess their is constraints that yiur input number should be between 0.00 to 500.00 so after input u need to check thats number is between given constraints or not Like this If( numb1>=0&&numb1<=500)
24th Oct 2021, 10:18 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
You need to use modf() function from <math.h>. The function's purpose is to break a floating point value into integral and fractional parts. http://www.cplusplus.com/reference/cmath/modf/ Once you extracted the integral parts of the two numbers, you can then convert them into int values before summing up.
24th Oct 2021, 10:58 AM
Ipang
+ 3
Simple. You change the numbers to integers before adding. Problem solved https://code.sololearn.com/cS1sQ3pd7TtA/?ref=app
24th Oct 2021, 11:03 AM
Rishi
Rishi - avatar
+ 3
Thank you
24th Oct 2021, 11:21 AM
morgan
morgan - avatar
+ 3
shravya Happy to help =)
25th Oct 2021, 12:45 AM
Rishi
Rishi - avatar
+ 2
Its working but I am not getting expected output when I keep my input as :-99.26 34.99 I should get output as :133 but I am getting it as 134
24th Oct 2021, 10:13 AM
morgan
morgan - avatar
+ 2
And there is another reason why I took int because it's given in mandatory test case 2 that it should be taken int sum;
24th Oct 2021, 10:43 AM
morgan
morgan - avatar
+ 1
It works fine, you just have an extra pound on the first line. Delete one
24th Oct 2021, 10:08 AM
Slick
Slick - avatar
+ 1
Ahh, that because the sum variable is an integer. An integer can't be a float unless cast, so switch the declaration from an int to a float shravya https://code.sololearn.com/cNjIWXyp1NEO/?ref=app
24th Oct 2021, 10:34 AM
Slick
Slick - avatar
+ 1
Then I am getting output like 134.25 but I want 133
24th Oct 2021, 10:40 AM
morgan
morgan - avatar