Java Program to Convert Milliliter to Liter and Liter to Milliliter

In the previous article, we have discussed about Java Program to Convert Centimeter to Meter and Kilometer

In this article we will see how to convert milliliter to liter and vice versa by using Java programming language.

Java Program to Convert Milliliter to Liter and Liter to Milliliter

Before jumping into the program let’s know the relationship between liter and milliliter and how we can convert milliliter to liter and vice-versa.

1 Liter = 1000 Milliliter

Formula to convert milliliter to liter.

Liter= Milliliter / 1000

Formula to convert liter to milliliter.

Milliliter = Liter * 1000

Let’s see different ways to find convert milliliter to liter and vice versa.

Method-1: Java Program to Convert Milliliter to Liter and Liter to Milliliter By Using Static Input Value

Approach:

  • Declare user input of milliliter and liter value.
  • Then convert milliliter to liter and vice versa by using the formula.
  • Print result.

Program:

public class Main 
{
   public static void main(String args[])
   {
        //value of milliliter declared
        double milliliter = 3600;
        //value of liter declared  
        double liter = 4.7;
        
        //calculating liter value
        double l  = milliliter/1000; 
        //calculating milliliter value
        double ml = liter*1000;
        //printing result
        System.out.println("Value in liter is: "+ l+" l");   
        System.out.println("Value in milliliter is: "+ ml+" ml");   
   }
}
Output:

Value in liter is: 3.6 l
Value in milliliter is: 4700.0 ml

Method-2: Java Program to Convert Milliliter to Liter and Liter to Milliliter By Using User Input Value

Approach:

  • Take user input of milliliter and liter value.
  • Then convert milliliter to liter and vice versa 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 milliliter 
        System.out.println("Enter value of milliliter: ");  
        double milliliter = sc.nextDouble();
        //Taking the value input of double variable liter 
        System.out.println("Enter value of liter: ");  
        double liter = sc.nextDouble();
        
        //calculating liter value
        double l  = milliliter/1000; 
        //calculating milliliter value
        double ml = liter*1000;
        //printing result
        System.out.println("Value in liter is: "+ l+" l");   
        System.out.println("Value in milliliter is: "+ ml+" ml");   
   }
}
Output:

Enter value of milliliter: 
4000
Enter value of liter: 
3.4
Value in liter is: 4.0 l
Value in milliliter is: 3400.0 ml

Method-3: Java Program to Convert Milliliter to Liter and Liter to Milliliter By Using User Defined Method

Approach:

  • Take user input of milliliter and liter value.
  • Call a user defined method by passing milliliter and liter value as parameter.
  • Inside method convert milliliter to liter and vice versa 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 milliliter 
        System.out.println("Enter value of milliliter: ");  
        double milliliter = sc.nextDouble();
        //Taking the value input of double variable liter 
        System.out.println("Enter value of liter: ");  
        double liter = sc.nextDouble();
        //calling user defined method convert()
        convert(milliliter,liter);
   }
   
   public static void convert(double milliliter, double liter)
   {
        //calculating liter value
        double l  = milliliter/1000; 
        //calculating milliliter value
        double ml = liter*1000;
        //printing result
        System.out.println("Value in liter is: "+ l+" l");   
        System.out.println("Value in milliliter is: "+ ml+" ml");   
   }
}
Output:

Enter value of milliliter: 
1000
Enter value of liter: 
1.5
Value in liter is: 1.0 l
Value in milliliter is: 1500.0 ml

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Related Java Programs: