Skip to content

Convert Kilometers To Miles

Updated: at 10:50 AM

Table of contents

Open Table of contents

Write a program to convert kilometers to miles

1 mile = 1.609344 kilometers

Input  : 8.04672
Output : 5

C

// Write a C program to convert kilometers to miles

#include <stdio.h>

int main()
{
    float kilometer;
    int miles;

    printf("Enter kilometer :    ");
    scanf("%f", &kilometer);

    miles = kilometer/1.609;

    printf("%f  kilometer is    :   %i  miles", kilometer, miles);

    return 0;
}

C#

// Write a C# program to convert kilometers to miles

using System;
class MainClass {
    public static void Main (string[] args) {
    Console.WriteLine ("Please enter the kilometer : ");
    double km = Convert.ToDouble(Console.ReadLine());
    double miles = km/1.609;
    Console.WriteLine(km+" kilometer is "+miles+" miles");
  }
}

C++

// Write a C++ program to convert kilometers to miles

#include <iostream>

using namespace std;

int main() {
    // Write C++ code here
    float miles,kilometers;
    cout << "enter kilometers"<<endl;
    cin>>kilometers;

    cout<<"miles "<<kilometers/1.609;

    return 0;
}

Dart

// Write a Dart program to convert kilometers to miles

import 'dart:io';
import 'dart:math';
void main(){
  print("Enter the kilometer :");
  var kilometer =double.parse(stdin.readLineSync());
  var miles = (kilometer / 1.60).round();
  print('Equivalent miles : $miles');
}

F#

// Write a F# program to convert kilometers to miles

let kilometerToMiles km : float =
   km / 1.60

let miles = kilometerToMiles 8.04672

printfn "Equivalent miles : %f" miles

Go

// Write a Go program to convert kilometers to miles

package main

import "fmt"

func main() {
	var km float64
	fmt.Scanf("%f", &km)
	miles := 0.621 * km
	fmt.Println(miles)
}

Haskell

-- Write a Haskell program to convert kilometers to miles

main :: IO ()
main = do
   putStrLn "Enter kilometers: "
   input1 <- getLine
   let kilometers = read input1 :: Float

   let miles = kilometers / 1.609344
   putStrLn $ show (miles)

Java

// Write a Java program to convert kilometers to miles

import java.util.Scanner;

public class ConvertKilometersToMiles{
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            System.out.print("Enter Kilometers:");
            double km = sc.nextDouble();

            double miles = km/(1.609);

            System.out.println("Distance in mile:" +miles);
        }
    }
}

JavaScript

// Write a JavaScript program to convert kilometers to miles

/**
 * Add two numbers together
 * @param  {Float} Kilometers a distance with kilometers unit
 *
 * @return {Float}    the distance with miles unit
 */

function ConvertKilometersToMiles(Kilometers) {
  return Kilometers / 1.609;
}

//test
console.log(ConvertKilometersToMiles(8.04672));

Julia

# Write a Julia program to convert kilometers to miles

println("Enter value in kilometers: ")
km = readline()
km = parse(Float64, km)
miles = round((km / 1.609); digits = 2)
println(km, " kilometers equals ", miles, " miles")

Kotlin

// Write a Kotlin program to convert kilometers to miles

import java.util.Scanner

fun main() {
    print("Enter Kilometers : ")
    println()
    var kilometers = readLine()

    println("Distance in mile : " + ConvertKilometersToMiles(kilometers!!.toDouble()))

}

private fun ConvertKilometersToMiles(km: Double): Double {
    val miles = km / 1.609;

    return  miles
}

Perl

# Write a Perl program to convert kilometers to miles

#!/usr/bin/perl
use warnings;
use strict;
print "Enter the kilometer : ";
my $km = <STDIN>;
my $miles = $km / 1.609;
print $km." kilometers are ".$miles." miles\n";

PHP

// Write a PHP program to convert kilometers to miles

<?php
$x = readline('Enter Kilometers: ');

$x1 = $x/1.609;
echo "Miles: $x1.\n";

?>

Python

# Write a Python program to convert kilometers to miles

def km_to_miles(value_in_km):
    return round(value_in_km / 1.609, 2)


print("Miles:\n", km_to_miles(float(input("Kilometers: "))))

R

# Write a R program to convert kilometers to miles

kilometers = 8.04672
miles = kilometers / 1.609344
cat("miles: ", miles)

Ruby

# Write a Ruby program to convert kilometers to miles

def kilometerToMiles(km)
  km / 1.609
end

if $PROGRAM_NAME == __FILE__
  print 'Please enter the Kilometer : '
  km = gets.chomp.to_i

  puts "#{km} kilometer is #{kilometerToMiles(km)} miles"
end

Scala

// Write a Scala program to convert kilometers to miles

import scala.io.StdIn._;

object kmtoMiles {
  def main(args:Array[String]):Unit={
    println("Enter the Kilometer : ");
    var km = readLine();
    var miles = km.toDouble / 1.609;
    println(km+" kilometer is "+miles+" miles");
  }
}

Swift

// Write a Swift program to convert kilometers to miles

import Swift
import Foundation

print("Enter kilometers: ")
var kilometers = Double(readLine()!)!
print("Miles: ", kilometers / 1.60934)

TypeScript

// Write a TypeScript program to convert kilometers to miles

/**
 *
 * @name This program convert kilometrs to miles
 * @param {number} kilometers
 * @returns {number}
 *
 * @example
 * ```ts
 * const mile1 = ConvertKilometersToMiles(8.04672) // 5
 * const mile2 = ConvertKilometersToMiles(16.08) // 10
 * ```
 *
 * @author Tanishq Singh
 */
const ConvertKilometersToMiles = (kilometers: number): number =>
  kilometers * 0.621371;

// Console Tests
console.log("Number of miles in 1.6 kilometers: ");
console.log("That is " + ConvertKilometersToMiles(1.6) + " // 0.9941936");

console.log("Numer of miles in 3.2 kilometers: ");
console.log("That is " + ConvertKilometersToMiles(3.2) + " // 1.9883872");

Similar programs