In the previous article, we have discussed about Java Program to Convert Foot to Kilometer and Kilometer to Foot
In this article we will see how to convert Foot to Meter and Meter to Foot by using Java programming language.
Java Program to Convert Foot to Meter and Meter to Foot
Before jumping into the program let’s know the relationship between Foot and Meter and how we can convert Foot to Meter and vice versa.
Generally Foot and Meter are used as unit in case of length/distance measurement.
1 Foot = 0.3048 Meter 1 Meter = 3.28084 Feet
Formula to convert Meter to Foot.
Foot = Meter * 3.28084
Formula to convert Foot to Meter.
Meter = Foot * 0.3048
Let’s see different ways to convert Foot to Meter and Meter to Foot.
Method-1: Java Program to Convert Foot to Meter and Meter to Foot By Using Static Input Value
Approach:
- Declare Foot and Meter value.
- Then convert Foot to Meter and Meter to Foot by using the formula.
- Print result.
Program:
public class Main { public static void main(String args[]) { //value of foot declared double foot = 1; //value of meter declared double meter = 1; //converting foot to meter double m = foot * 0.3048; //converting meter to foot double f = meter * 3.28084; //printing result System.out.println("Value of "+meter+" meter in foot: "+ f); System.out.println("Value of "+foot+" foot in meter: "+ m); } }
Output: Value of 1.0 meter in foot: 3.28084 Value of 1.0 foot in meter: 0.3048
Method-2: Java Program to Convert Foot to Kilometer and Kilometer to Foot By Using User Input Value
Approach:
- Take user input of Foot and Meter value.
- Then convert Foot to Meter and Meter to Foot 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 meter System.out.println("Enter value of meter: "); double meter = sc.nextDouble(); //Taking the value input of double variable foot System.out.println("Enter value of foot: "); double foot = sc.nextDouble(); //converting foot to meter double m = foot * 0.3048; //converting meter to foot double f = meter * 3.28084; //printing result System.out.println("Value of "+meter+" meter in foot: "+ f); System.out.println("Value of "+foot+" foot in meter: "+ m); } }
Output: Enter value of meter: 2500 Enter value of foot: 300 Value of 2500.0 meter in foot: 8202.1 Value of 300.0 foot in meter: 91.44
Method-3: Java Program to Convert Foot to Kilometer and Kilometer to Foot By Using User Defined Method
Approach:
- Take user input of Foot and Meter value.
- Call a user defined method by passing Foot and Meter value as parameter.
- Inside method convert Foot to Meter and Meter to Foot 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 meter System.out.println("Enter value of meter: "); double meter = sc.nextDouble(); //Taking the value input of double variable foot System.out.println("Enter value of foot: "); double foot = sc.nextDouble(); //calling user defined method convert() convert(meter, foot); } //convert() method to convert meter to foot and vice versa public static void convert(double meter, double foot) { //converting foot to meter double m = foot * 0.3048; //converting meter to foot double f = meter * 3.28084; //printing result System.out.println("Value of "+meter+" meter in foot: "+ f); System.out.println("Value of "+foot+" foot in meter: "+ m); } }
Output: Enter value of meter: 5 Enter value of foot: 5 Value of 5.0 meter in foot: 16.4042 Value of 5.0 foot in meter: 1.524
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Related Java Programs: