Java Program to Convert Kilogram to Metric Ton and Metric Ton to Kilogram

In the previous article, we have discussed about Java Program to Convert Kilogram to Gram and Gram to Kilogram

In this article we will see how to convert Kilogram to Metric Ton and Metric Ton to Kilogram by using Java programming language.

Java Program to Convert Kilogram to Metric Ton and Metric Ton to Kilogram

Before jumping into the program let’s know the relationship between Kilogram and Metric Ton and how we can convert Kilogram to Metric Ton and vice versa.

Generally Kilogram and Metric Ton are used as unit in case of  mass/weight measurement.

1 Kilogram = 0.001 Metric Ton
1 Metric Ton =  1000 Kilogram

Formula to convert Kilogram to Metric Ton.

Metric Ton  = Kilogram * 0.001  (OR)  Kilogram / 1000

Formula to convert Metric Ton to Kilogram.

Kilogram  = Metric Ton * 1000

Let’s see different ways to convert Kilogram to Metric Ton and Metric Ton to Kilogram.

Method-1: Java Program to Convert Kilogram to Metric Ton and Metric Ton to Kilogram By Using Static Input Value

Approach:

  • Declare Kilogram and Metric Ton value.
  • Then convert Kilogram to Metric Ton and Metric Ton to Kilogram by using the formula.
  • Print result.

Program:

public class Main 
{
   public static void main(String args[])
    {
        //value of kilogram declared
        double kilogram = 1;
        //value of metric ton declared
        double metricTon = 1;

        //converting kilogram to metric ton
        double t = kilogram * 0.001;
        //converting metric ton to kilogram
        double kg = metricTon * 1000;
        //printing result
        System.out.println("Value of "+kilogram+" kilogram in metric ton: "+ t);   
        System.out.println("Value of "+metricTon+" metric ton in kilogram: "+ kg);   
   }
}
Output:

Value of 1.0 kilogram in metric ton: 0.001
Value of 1.0 metric ton in kilogram: 1000.0

Method-2: Java Program to Convert Kilogram to Metric Ton and Metric Ton to Kilogram By Using User Input Value

Approach:

  • Take user input of Kilogram and Metric Ton value.
  • Then convert Kilogram to Metric Ton and Metric Ton to Kilogram by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable kilogram
        System.out.println("Enter value of kilogram: ");  
        double kilogram = sc.nextDouble();
        //Taking the value input of double variable metricTon
        System.out.println("Enter value of metric ton: ");  
        double metricTon = sc.nextDouble();

        //converting kilogram to metric ton
        double t = kilogram * 0.001;
        //converting metric ton to kilogram
        double kg = metricTon * 1000;
        //printing result
        System.out.println("Value of "+kilogram+" kilogram in metric ton: "+ t);   
        System.out.println("Value of "+metricTon+" metric ton in kilogram: "+ kg);   
   }
}
Output:

Enter value of kilogram: 
2000
Enter value of metric ton: 
2
Value of 200.0 kilogram in metric ton: 0.2
Value of 2.0 metric ton in kilogram: 2000.0

Method-3: Java Program to Convert Kilogram to Metric Ton and Metric Ton to Kilogram By Using User Defined Method

Approach:

  • Take user input of Kilogram and Metric Ton value.
  • Call a user defined method by passing Kilogram and Metric Ton value as parameter.
  • Inside method convert Kilogram to Metric Ton and Metric Ton to Kilogram by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable kilogram
        System.out.println("Enter value of kilogram: ");  
        double kilogram = sc.nextDouble();
        //Taking the value input of double variable metricTon
        System.out.println("Enter value of metric ton: ");  
        double metricTon = sc.nextDouble();
        //calling user defined method convert()
        convert(kilogram, metricTon);
   }
   
   //convert() method to convert kilogram to metric ton and vice versa
   public static void convert(double kilogram, double metricTon)
   {
        //converting kilogram to metric ton
        double t = kilogram * 0.001;
        //converting metric ton to kilogram
        double kg = metricTon * 1000;
        //printing result
        System.out.println("Value of "+kilogram+" kilogram in metric ton: "+ t);   
        System.out.println("Value of "+metricTon+" metric ton in kilogram: "+ kg);   
   }
}
Output:

Enter value of kilogram: 
2000
Enter value of metric ton: 
2
Value of 200.0 kilogram in metric ton: 0.2
Value of 2.0 metric ton in kilogram: 2000.0

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: