+ 3

Problem with decimal values

Help,I know I am doing something wrong here and the answer is pretty obvious yet I can't figure it out. I think the output should have been a decimal value(upto some decimal point if cpp cant handle irrational values).But the output is integer value much like modulus(%) //code here https://code.sololearn.com/c2Ld7UjF2FfH/?ref=app

27th Oct 2020, 6:56 AM
Ice_Bear
Ice_Bear - avatar
7 Respuestas
+ 4
(i) Only one of them need to be float. So instead of 22 write 22.0 or float(22). (ii) float pi = int(22) / int(7); => float pi = 3; => cout << pi; // would be 3.
27th Oct 2020, 7:14 AM
Soheil
Soheil - avatar
+ 4
(int) / (int) will result in int. Write (float)22 / 7 then you will see 3.14!
27th Oct 2020, 7:00 AM
Soheil
Soheil - avatar
+ 2
thanks soheil, that answered my query regarding the output of a integer in the cout statement. Modifying that line will now result in a decimal value cout<<22.0/7; output-3.14 //and not cout<<22/7; resulting in a integer (3) //thanks for reminding me that concept but what about this statement float pi; pi=22/7; (i)do I still have to convert 22 to 22.0 and 7 in the similar way? (ii)isn't pi already declared a float type so by my guess(which could be wrong here) it should print out a decimal containing value?
27th Oct 2020, 7:10 AM
Ice_Bear
Ice_Bear - avatar
+ 1
ahh ok! so for float, either values should contain a decimal or float(any integer here) both are valid. That was really helpful thanks and have a great day ahead!
27th Oct 2020, 7:20 AM
Ice_Bear
Ice_Bear - avatar
28th Oct 2020, 8:36 PM
Sauron24
Sauron24 - avatar
0
Ice_Bear You're welcome :))
27th Oct 2020, 7:22 AM
Soheil
Soheil - avatar