In: Computer Science
Dear Learner,
Here is the C++ code for converting gallons to liters.
NOTE:- the formula for converting used in the code is 1 (US)Gallon = 3.78541 Liters
CODE:
#include<iostream>
using namespace std;
main()
{ float gallons,liters; //variables to store the quantities
cout<<"Enter the quantity in gallons :"; //asking the user to
enter the volume in gallons
cin>>gallons; //storing the input in 'gallons' variable. NOTE
that the float datatype will allow the user to enter amount with
decimal places
liters = 3.78541 * gallons; //calculating corresponding
liters
cout<<"\n"<<gallons<<" gallons =
"<<liters<<" liters"; //displaing the result
}
SCREENSHOT:
OUTPUT:
I hope I have answered the question the way you were expecting. If you still have any doubts or want any other explanation, feel free to ask us in the comments.
Please leave a like if this was helpful.
Thanks,
Happy Studying.