Convert Grams To Kilograms

In this section, you will learn to convert Grams To kilograms. The following program helps you in converting Grams To kilograms.

Ads

Convert Grams To Kilograms

     

In this section, you will learn to convert Grams To kilograms. The following program helps you in converting Grams To kilograms. 

Code Description:

To convert Grams To kilograms we have used a formula "kilogram = gram * 0.001;". The compiler will ask to enter the number of Grams from the keyboard due to the method  "
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));". Here we have taken gram to be of type int. To avoid loss of precession we have cast the output to be in double.

Here is the code of the program:

import java.io.*;
import java.util.*;

class GmsTokgs{
  public static void main (String[] args)throws Exception{
  //1 gram = 0.001 kilogram;
  BufferedReader bf = new BufferedReader(new 
  InputStreamReader
(System.in));
  System.out.print("Enter the Grams:");
  int gram = Integer.parseInt(bf.readLine());
  double kilogram = gram * 0.001;
  System.out.println("kilogram: " + kilogram);
  }
}

Output of the program:

C:\unique>javac GmsTokgs.java

C:\unique>java GmsTokgs
Enter the Grams:1
kilogram: 0.0010

C:\unique>java GmsTokgs
Enter the Grams:10
kilogram: 0.01

C:\unique>

Download this example.

Advertisements

Ads
Share on Google+Share on Google+

Convert Grams To Kilograms

Posted on: February 27, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Convert Grams To Kilograms  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads