Java Program to Convert Centimeter to Millimeter and Millimeter to Centimeter

In this article we will see how to convert Centimeter to Millimeter and Millimeter to Centimeter by using Java programming language.

Java Program to Convert Centimeter to Millimeter and Millimeter to Centimeter

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

Generally Centimeter and Millimeter are used as unit in case of distance measurement.

1 Centimeter =   10 Millimeter
1 Millimeter =  0.1 Centimeter

Formula to convert Centimeter to Millimeter.

Millimeter = Centimeter * 10

Formula to convert Millimeter to Centimeter.

Centimeter  = Millimeter * 0.1

Let’s see different ways to convert Centimeter to Millimeter and Millimeter to Centimeter.

Method-1: Java Program to Convert Centimeter to Millimeter and Millimeter to Centimeter By Using Static Input Value

Approach:

  • Declare Centimeter and Millimeter value.
  • Then convert Centimeter to Millimeter and Millimeter to Centimeter by using the formula.
  • Print result.

Program:

public class Main 
{
   public static void main(String args[])
   {
        //value of centimeter declared
        double centimeter = 1;
        //value of millimeter declared  
        double millimeter = 1;
        
        //converting centimeter to millimeter
        double mm = centimeter * 10; 
        //converting millimeter to centimeter
        double cm = millimeter * 0.1;
        //printing result
        System.out.println("Value of "+centimeter+" centimeter in millimeter: "+ mm);   
        System.out.println("Value of "+millimeter+" millimeter in centimeter: "+ cm);   
   }
}
Output:

Value of 1.0 centimeter in millimeter: 10.0
Value of 1.0 millimeter in centimeter: 0.1

Method-2: Java Program to Convert Centimeter to Millimeter and Millimeter to Centimeter By Using User Input Value

Approach:

  • Take user input of Centimeter and Millimeter value.
  • Then convert Centimeter to MillimeterĀ and Millimeter to Centimeter 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 centimeter
        System.out.println("Enter value of centimeter: ");  
        double centimeter = sc.nextDouble();
        //Taking the value input of double variable millimeter
        System.out.println("Enter value of millimeter: ");  
        double millimeter = sc.nextDouble();
        
        //converting centimeter to millimeter
        double mm = centimeter * 10; 
        //converting millimeter to centimeter
        double cm = millimeter * 0.1;
        //printing result
        System.out.println("Value of "+centimeter+" centimeter in millimeter: "+ mm);   
        System.out.println("Value of "+millimeter+" millimeter in centimeter: "+ cm);   
   }
}
Output:

Enter value of centimeter: 
2
Enter value of millimeter: 
1
Value of 1.0 centimeter in millimeter: 20.0
Value of 1.0 millimeter in centimeter: 0.1

Method-3: Java Program to Convert Centimeter to Millimeter and Millimeter to Centimeter By Using User Defined Method

Approach:

  • Take user input of Centimeter and Millimeter value.
  • Call a user defined method by passing Centimeter and Millimeter value as parameter.
  • Inside method convert Centimeter to Millimeter and Millimeter to Centimeter 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 centimeter
        System.out.println("Enter value of centimeter: ");  
        double centimeter = sc.nextDouble();
        //Taking the value input of double variable millimeter
        System.out.println("Enter value of millimeter: ");  
        double millimeter = sc.nextDouble();
         //calling user defined method convert()
        convert(centimeter, millimeter);
   }
   
   //convert() method to convert centimeter to millimeter and vice versa
   public static void convert(double centimeter, double millimeter)
   {
        //converting centimeter to millimeter
        double mm = centimeter * 10; 
        //converting millimeter to centimeter
        double cm = millimeter * 0.1;
        //printing result
        System.out.println("Value of "+centimeter+" centimeter in millimeter: "+ mm);   
        System.out.println("Value of "+millimeter+" millimeter in centimeter: "+ cm);   
   }
}
Output:

Enter value of centimeter: 
10
Enter value of millimeter: 
25
Value of 10.0 centimeter in millimeter: 100.0
Value of 25.0 millimeter in centimeter: 2.5

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Leave a Comment