Java Program to Convert Kilogram to Gram and Gram to Kilogram

In the previous article, we have discussed about Java Program to Convert Foot to Yard and Yard to Foot

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

Java Program to Convert Kilogram to Gram and Gram to Kilogram

Before jumping into the program let’s know the relationship between Kilogram and Gram and how we can convert Kilogram to Gram (Kg To Gram)
and (Gram To Kg or Gm To Kg) Gram To Kilogram vice versa.

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

1 Kilogram =  1000 Gram
1 Gram = 0.001 Kilogram

Formula to convert Kilogram to Gram.

Gram  = Kilogram * 1000

Formula to convert Gram to Kilogram.

Kilogram  = Gram * 0.001 (OR) Gram / 1000

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

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

Approach:

  • Declare Kilogram and Gram value.
  • Then convert Kilogram to Gram and Gram 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 gram declared
        double gram = 1;
        
        //converting kilogram to gram
        double gm = kilogram * 1000;
        //converting gram to kilogram
        double kg = gram * 0.001;
        //printing result
        System.out.println("Value of "+kilogram+" kilogram in gram: "+ gm);   
        System.out.println("Value of "+gram+" gram in kilogram: "+ kg);   
   }
}
Output:

Value of 1.0 kilogram in gram: 1000.0
Value of 1.0 gram in kilogram: 0.001

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

Approach:

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

Enter value of kilogram: 
4
Enter value of gram: 
750
Value of 4.0 kilogram in gram: 4000.0
Value of 750.0 gram in kilogram: 0.75

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

Approach:

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

Enter value of kilogram: 
12.5
Enter value of gram: 
1700
Value of 12.5 kilogram in gram: 12500.0
Value of 1700.0 gram in kilogram: 1.7

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.

If you have succeeded answering these questions you can decide yourself you learn the concept very well.

  1. Write A Program To Convert Kg To G In Java
  2. How To Convert Gram To Kilogram
  3. How Many Grams In A Kilogram
  4. How To Convert Kilograms To Grams
  5. Convert Kilogram To Grams
  6. Convert Kilograms To Grams
  7. Write A Program To Convert Grams Into Kilograms
  8. Convert Kg Into Gram
  9. Convert Kg To Grams
  10. How To Convert Grams To Kilograms In Excel
  11. Convert Gram To Kilogram
  12. Kilogram Na Gram
  13. Kilogram To Gram Formula
  14. Gram In Kg
  15. Van Gram Naar Kg
  16. Gram Naar Kilogram
  17. Kg To Gr
  18. Van Gram Naar Kg
  19. 1000 Gram To Kg

Related Java Programs:

Leave a Comment