# Robson » Little Red Book » Units conversion #2

## Chapter 2 - Question 2

Write programs to convert metric units to imperial for each of the following pairs:

• centimetres to inches
• metres to feet
• kilometres to miles
• grams to ounces
• kilograms to pounds

Input is to be a number representing a quantity in the metric unit given by the program; output is to be equivilant amount and the imperial unit involved.

### Solution 1

This is just a reversed version of problem 2.1.

`#include <stdio.h>`
` `
`int main(int argc, char *argv[])`
`{`
`    bool bExit = false;`
`    char *cFrom[] =  { "centimetres", "metres", "kilometers", "grams",    "kilograms" };`
`    char *cTo[]   =  { "inches",      "feet",   "miles",      "ounces",   "pounds"    };    `
`    float fRatio[] = { 0.393,         3.2808,   0.62137,      0.0352739,  2.20462     };`
`    int iType;`
`    float fAmount;`
` `
`    while (!bExit)`
`    {`
`        for (int n = 0; n < 5; n++)`
`            printf(" %d. Convert %s to %s\n", n+1, cFrom[n], cTo[n]);`
` `
`        printf("\nType a number from 1 to 5 or anything else to exit.\n>");`
`        scanf("%d", &iType);`
`        `
`        if (iType >= 1 && iType <= 5)`
`        {`
`            printf("\nHow many %s?\n>", cFrom[iType-1]);`
`            scanf("%f", &fAmount);`
`            printf("\n%g %s is %g %s.\n\n- - -\n\n", fAmount, cFrom[iType-1], fAmount * fRatio[iType-1], cTo[iType-1]);`
`        }  `
`        else`
`            bExit = true; `
`    }`
`    `
`    return 0;`
`}`

## Log

• May 2, 2005 - Added solution 1.
© 2004-19 robson | cc unless stated