Home Java Java-conversion Convert Grams To Ounce
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Convert Grams To Ounce

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

Convert Grams To Ounce

     

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

Code Description:

To convert Grams To Ounce we have used a formula "ounce = gram * 0.035273962;". 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 Grams 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 GmsToOunce {
  public static void main(String[] argsthrows Exception{
  //1 gram = 0.035 273 962 ounce
  BufferedReader bf = new BufferedReader(new 
  InputStreamReader
(System.in));
  System.out.println("Enter the Grams:");
  int Grams = Integer.parseInt(bf.readLine());
  double Ounce = Grams * 0.035273962;  
  System.out.println("Ounce:" + Ounce);
  }
}

Output of the program:

C:\unique>javac GmsToOunce.java

C:\unique>java GmsToOunce
Enter the Grams:
2
Ounce:0.070547924

C:\unique>

Download this example.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 27, 2008

Ask Questions?    Discuss: Convert Grams To Ounce  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com