Java Program to Convert Centimeter to Yard and Yard to Centimeter

In the previous article, we have discussed about Java Program to Convert Centimeter to Nautical Mile and Nautical Mile to Centimeter

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

Java Program to Convert Centimeter to Yard and Yard to Centimeter

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

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

1 Centimeter =  0.0109361 Yard
1 Yard =  91.44 Centimeter

Formula to convert Centimeter to Yard.

Yard = Centimeter * 0.0109361

Formula to convert Yard to Centimeter.

Centimeter  = Yard * 91.44

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

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

Approach:

  • Declare Centimeter and Yard value.
  • Then convert Centimeter to Yard and Yard 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 yard declared  
        double yard = 1;
        
        //converting centimeter to yard
        double y = centimeter * 0.0109361; 
        //converting yard to centimeter
        double cm = yard * 91.44;
        //printing result
        System.out.println("Value of "+centimeter+" centimeter in yard: "+ y);   
        System.out.println("Value of "+yard+" yard in centimeter: "+ cm);   
   }
}
Output:

Value of 1.0 centimeter in yard: 0.0109361
Value of 1.0 yard in centimeter: 91.44

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

Approach:

  • Take user input of Centimeter and Yard value.
  • Then convert Centimeter to Yard and Yard 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 yard
        System.out.println("Enter value of yard: ");  
        double yard = sc.nextDouble();
        
        //converting centimeter to yard
        double y = centimeter * 0.0109361; 
        //converting yard to centimeter
        double cm = yard * 91.44;
        //printing result
        System.out.println("Value of "+centimeter+" centimeter in yard: "+ y);   
        System.out.println("Value of "+yard+" yard in centimeter: "+ cm);   
   }
}
Output:

Enter value of centimeter: 
5000
Enter value of yard: 
5
Value of 5000.0 centimeter in yard: 54.6805
Value of 5.0 yard in centimeter: 457.2

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

Approach:

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

Enter value of centimeter: 
4500
Enter value of yard: 
4.5
Value of 4500.0 centimeter in yard: 49.212450000000004
Value of 4.5 yard in centimeter: 411.48

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Related Java Programs: