Java Program to Convert Kilogram to Ounce and Ounce to Kilogram

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

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

Java Program to Convert Kilogram to Ounce and Ounce to Kilogram

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

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

1 Kilogram =  35.274 Ounce
1 Ounce = 0.0283495 Kilogram

Formula to convert Kilogram to Ounce.

Ounce  = Kilogram *  35.274

Formula to convert Ounce to Kilogram.

Kilogram  = Ounce * 0.0283495 (OR) Ounce / 35.274

Let’s see different ways to convert Kilogram to Ounce and Ounce to Kilogram.

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

Approach:

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

Program:

public class Main 
{
   public static void main(String args[])
   {
        //value of square kilogram declared
        double kilogram = 1;
        //value of ounce declared
        double ounce = 1;
        
        //converting kilogram to ounce
        double o = kilogram * 35.274;
        //converting ounce to kilogram
        double kg = ounce * 0.0283495;
        //printing result
        System.out.println("Value of "+kilogram+" kilogram in ounce: "+ o);   
        System.out.println("Value of "+ounce+" ounce in kilogram: "+ kg);   
   }
}
Output:

Value of 1.0 kilogram in ounce: 35.274
Value of 1.0 ounce in kilogram: 0.0283495

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

Approach:

  • Take user input of Kilogram and Ounce value.
  • Then convert Kilogram to Ounce and Ounce 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 ounce
        System.out.println("Enter value of ounce: ");  
        double ounce = sc.nextDouble();
        
        //converting kilogram to ounce
        double o = kilogram * 35.274;
        //converting ounce to kilogram
        double kg = ounce * 0.0283495;
        //printing result
        System.out.println("Value of "+kilogram+" kilogram in ounce: "+ o);   
        System.out.println("Value of "+ounce+" ounce in kilogram: "+ kg);   
   }
}
Output:

Enter value of kilogram: 
2
Enter value of ounce: 
20
Value of 2.0 kilogram in ounce: 70.548
Value of 20.0 ounce in kilogram: 0.56699

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

Approach:

  • Take user input of Kilogram and Ounce value.
  • Call a user defined method by passing Kilogram and Ounce value as parameter.
  • Inside method convert Kilogram to Ounce and Ounce 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 ounce
        System.out.println("Enter value of ounce: ");  
        double ounce = sc.nextDouble();
        //calling user defined method convert()
        convert(kilogram, ounce);
   }
   
   //convert() method to convert kilogram to ounce and vice versa
   public static void convert(double kilogram, double ounce)
   {
        //converting kilogram to ounce
        double o = kilogram * 35.274;
        //converting ounce to kilogram
        double kg = ounce * 0.0283495;
        //printing result
        System.out.println("Value of "+kilogram+" kilogram in ounce: "+ o);   
        System.out.println("Value of "+ounce+" ounce in kilogram: "+ kg);   
   }
}
Output:

Enter value of kilogram: 
20
Enter value of ounce: 
5
Value of 20.0 kilogram in ounce: 705.48
Value of 5.0 ounce in kilogram: 0.1417475

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Programs: