# C programming - How to print numbers with a decimal component using only loops?

I'm currently taking a basic intro to C programming class, and for our current assignment I am to write a program to convert the number of kilometers to miles using loops--no if-else, switch statements, or any other construct we haven't learned yet are allowed. So basically we can only use loops and some operators. The program will generate three identical tables (starting from 1 kilometer through the input value) for one number input using the while loop for the first set of calculations, the for loop for the second, and the do loop for the third.

I've written the entire program, however I'm having a bit of a problem with getting it to recognize an input with a decimal component.

The code reads in and converts integers fine, but because the increment only increases by 1 it won't print a number with a decimal component (e.g. 3.2, 22.6, etc.).

Can someone point me in the right direction on this? I'd really appreciate any help! :)

It's not clear what you're trying to get as output. You use the example of starting with 3.2, so based from that, and based on your current program, your output is:

KILOMETERS       MILES    (while loop)
==========       =====
1.000          0.620
2.000          1.240
3.000          1.860

Is the problem that your table ends without putting out a value for 3.2? A simple way to solve that (only using loop statements, per your requirement) would be to add the following code:

while (count < km)
{
printf ("%8.3lf %14.3lf\n", km, KM_TO_MILE * km);
count = km;
}

It's really an if statement in disguise, and I don't know if your prof would accept it, but it does provide the final line of output.

Are you required to put out entries that increase by 1.0 (km) for each line? If not, perhaps a better solution is to determine an offset from one line to the next which lets you iterate between 1.0 and km, over a finite number of rows.

A simple solution is multiplying the input (km) by 10, 100 or 1000. Then dividing the result (mi) by the same constant. But avoid printing the result in the loop, take it out of there. You may lose precision, though (that's your next exercise :)

#include <stdio.h>
#define KM_TO_MILE .62

#define NAMEITHOWYOULIKE 100.0

main (void)
{
double km, mi, count;

printf ("This program converts kilometers to miles.\n");

do
{
printf ("\nEnter a positive non-zero number");
printf (" of kilometers of the race:  ");
scanf  ("%lf", &km);
getchar();
}while (km <= 1);

km = km * NAMEITHOWYOULIKE;

printf ("\n KILOMETERS       MILES    (while loop)\n");
printf (" ==========       =====\n");

count = 1;
while (count <= km)
{
mi = KM_TO_MILE * count;
++count;
}
printf ("%8.3lf %14.3lf\n", count/NAMEITHOWYOULIKE, mi/NAMEITHOWYOULIKE);
getchar();
}

If you want this for output:

KILOMETERS       MILES    (while loop)
==========       =====
1.000          0.620
2.000          1.240
3.000          1.860
3.200          1.984

Just add the following after your while loop (for an exclusive "final result"):

mi = KM_TO_MILE * km;
printf("%8.3lf %14.3lf\n", km, mi);

or an inclusive "final result" to prevent the last two results being the same if a whole number is used:

while (km > count)
{
mi = KM_TO_MILE * km;
printf("%8.3lf %14.3lf\n", km, mi);
break;
}

You successfully printed the whole number values, but need to print the final value when that is complete. All you need to do at the end (above) is convert the value directly from kilometers to miles.