C Program to Convert Kilometers to Meters

In this article, we will write a C program to convert kilometers to meters.

The program takes the value of the distance in kilometers from the user and prints the result in meters.

Sample Input:

Enter the value of the distance in km: 1.5

Output:

The distance in meters is: 1500.00

One kilometer is equal to 1000 meters. It means, to convert a given value of kilometers into meters, we have to multiply it by 1000.

meters = 1000 * kilometers

The following C program shows how you can convert kilometers to meters:

// C program to convert kilometers to meters
#include <stdio.h>

int main(){
    
    // Declare the variables
    float km, m;
    
    printf("Enter the value of the distance in km: ");
    scanf("%f", &km);
    
    // Convert km to m by multiplying 1000
    m = 1000 * km;
    
    // Print the result(up to 2 decimal points only)
    printf("The distance in meters is: %.2f", m);
    
    return 0;
    
}

Output:

Enter the value of the distance in km: 1.5
The distance in meters is: 1500.00

Code Explanation:

  • The program takes the value of the distance in kilometers from the user using the scanf() function and stores it in the km variable.
  • To get the distance in meters, multiply the value of the km variable by 1000 and store the result in the m variable.
  • Print the value of the m variable up to only two decimal points using the format specifier. The format specifier .2f indicates that the floating-point number should be formatted with 2 decimal places only.

Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.