Gallons to liters

Hey guys, I'm back. I was doing quite well on my own for awhile too!
-Anyway, I'm writing a program that converts gallons to liters using a FOR and a WHILE loop, my for loop works fine but my while loops seems to give me hell. Please point out my obvious mistakes!

PS:
I think my main problem is the math on my final product, ie 11 and 10 gallons have the same liters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include <stdio.h>
#define GtoL 3.785 //Liters per gallon.
int main()
{
	float liters; //liters cornverted by gallons.
	int count; //number of gallons to be converted to liters.
	count=10; //starting number of gallons to be converted.
	liters = count*GtoL; //equation for converting gallons to liters.
	while (count<=20)
{
	printf("%d gallons = %.3f liters\n",count, liters);
	liters = count*GtoL;
	count++;
}
return 0;
}
Last edited on
You're incrementing count after calculating liters -- so your liters variable will always "lag" behind your count variable.

So move line 14 above line 13.
This is why I love this site! Thank you so much!
Topic archived. No new replies allowed.