This blog is under construction

Sunday, 7 July 2013

C program to convert kilometers to miles, meters and feet

Write a C program to convert kilometers to miles, meters and feet.


  #include <stdio.h>
  #define KMTOMILE 1.60934
  #define METER 1
  #define METERTOFEET 3.28084

  int main() {
        float km, meter = 0, feet, mile = 0;

        /* get the number of kilometers from the user */
        printf("Enter the value in kilometers:");
        scanf("%f", &km);

        printf("%.2f kilometer = ", km);

        /* calculate the number of miles */
        if (km >= KMTOMILE) {
                while (1) {
                        mile++;
                        km = km - KMTOMILE;
                        if (km < KMTOMILE)
                                break;
                }
        }

        /* kilometer to feet conversion */
        feet = km * 1000 * METERTOFEET;

        /* calculate the number of meter */
        if (feet >= METERTOFEET) {
                while (1) {
                        meter++;
                        feet = feet - METERTOFEET;
                        if (feet < METERTOFEET)
                                break;
                }
        }
        /* print the output */
        printf("%.2f miles, %.2f meter and %.f feet\n",
                                        mile, meter, feet);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the value in kilometers:100
  100.00 kilometer = 62.00 miles, 220.00 meter and 3 feet





See Also:

1 comment: