I have to write a java program . So basically I have to take my input in only cm
ID: 3937902 • Letter: I
Question
I have to write a java program . So basically I have to take my input in only cms. Inches is also accepted but the inches should be converted into the cms before the program take in the input . So when the program takes the input , the input must be in cm . I have to write a java program . So basically I have to take my input in only cms. Inches is also accepted but the inches should be converted into the cms before the program take in the input . So when the program takes the input , the input must be in cm .Explanation / Answer
import java.io.*;
import java.util.*;
class InchesToCms{
public static void main (String[] args)throws Exception{
//1 Inches = 2.54 Centimeters;
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));//to get input creating bf is an object.
System.out.print("Enter the inches: ");//asking the input is inches
float inches = Float.parseFloat(bf.readLine());//geting input first in string,so convert into float using parseFloat function.
float cms = inches * 2.54f;//multiplying given input inches and 2.54.
System.out.println(inches + " inches is " + cms + " centimeter(s).");//display the inches and centimeters.
System.out.print("Enter the Centimeters: ");//asking the input is Centimeters.
float CMS = Float.parseFloat(bf.readLine());
System.out.println("You entered " +CMS+ " centimeter(s).");//display out put in Centimeters only.
}
}
/* Output:
1) Enter the inches: 11.0
11.0 inches is 27.939999 centimeter(s).
Enter the Centimeters: 2.0
You entered 2.0 centimeter(s).
Explanatin:
This program 1) It takes input in inches and convert into centimeters
2) and Next It takes input in centimters and display the out in the same centimeters.
*/