Seeing that 1 mile is equal to 1609 meters or 1760 yards, and that 1 yard is equal to 3
feet and that 1 foot is equal to 12 inches, write a program that reads a real number of kilometers and converts it to miles, yards, feets (all 3 in integer) and inches (in real rounded to 2 decimals after the comma).
Example :
– 3.846 km is equal to 2 miles, 686 yards, 2 feet and 9.70 inches.
– 1.609 km is equal to 1 mile, 0 yard, 0 feet and 0.00 inch.

 

Dislay on the screen the values separated by a comma, followed by a new line.


Difficulty level
This exercise is mostly suitable for students
#include <stdio.h>
#include <math.h>
int main()
{
    double km, inches;
    int m, y , f;
    
    scanf("%lf",&km);
    
    
    m  = km *1000 / 1609; 
    y =  (km *1000 -m*1609) * 1760 / 1609; 
    f = ((km *1000 -m*1609) * 1760 / 1609  - y )*3;
    inches= (((km *1000 -m*1609) * 1760 / 1609  - y )*3 -f)*12;
    
    printf("%d %d %d %.2lf\n",m , y, f , inches);
 

    return 0;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Sorting using heap-sort algorithm