In the previous article, we have discussed about Java Program to Convert Centimeter to Yard and Yard to Centimeter
In this article we will see how to convert Centimeter to Foot and Foot to Centimeter by using Java programming language.
Java Program to Convert Centimeter to Foot and Foot to Centimeter
Before jumping into the program let’s know the relationship between Centimeter and Foot and how we can convert Centimeter to Foot and vice versa.
Generally Centimeter and Foot are used as unit in case of distance measurement.
1 Centimeter = 0.0328084 Foot 1 Foot = 30.48 Centimeter
Formula to convert Centimeter to Foot.
Foot = Centimeter * 0.0328084
Formula to convert Foot to Centimeter.
Centimeter = Foot * 30.48
Let’s see different ways to convert Centimeter to Foot and Foot to Centimeter.
Method-1: Java Program to Convert Centimeter to Foot and Foot to Centimeter By Using Static Input Value
Approach:
- Declare Centimeter and Foot value.
- Then convert Centimeter to Foot and Foot to Centimeter by using the formula.
- Print result.
Program:
public class Main { public static void main(String args[]) { //value of centimeter declared double centimeter = 1; //value of foot declared double foot = 1; //converting centimeter to foot double f = centimeter * 0.0328084; //converting foot to centimeter double cm = foot * 30.48; //printing result System.out.println("Value of "+centimeter+" centimeter in foot: "+ f); System.out.println("Value of "+foot+" foot in centimeter: "+ cm); } }
Output: Value of 1.0 centimeter in foot: 0.0328084 Value of 1.0 foot in centimeter: 30.48
Method-2: Java Program to Convert Centimeter to Foot and Foot to Centimeter By Using User Input Value
Approach:
- Take user input of Centimeter and Foot value.
- Then convert Centimeter to Foot and Foot to Centimeter 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 centimeter System.out.println("Enter value of centimeter: "); double centimeter = sc.nextDouble(); //Taking the value input of double variable foot System.out.println("Enter value of foot: "); double foot = sc.nextDouble(); //converting centimeter to foot double f = centimeter * 0.0328084; //converting foot to centimeter double cm = foot * 30.48; //printing result System.out.println("Value of "+centimeter+" centimeter in foot: "+ f); System.out.println("Value of "+foot+" foot in centimeter: "+ cm); } }
Output: Enter value of centimeter: 80000 Enter value of foot: 6.5 Value of 8000.0 centimeter in foot: 262.4672 Value of 6.5 foot in centimeter: 198.12
Method-3: Java Program to Convert Centimeter to Foot and Foot to Centimeter By Using User Defined Method
Approach:
- Take user input of Centimeter and Foot value.
- Call a user defined method by passing Centimeter and Foot value as parameter.
- Inside method convert Centimeter to Foot and Foot to Centimeter 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 centimeter System.out.println("Enter value of centimeter: "); double centimeter = 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(centimeter, foot); } //convert() method to convert centimeter to foot and vice versa public static void convert(double centimeter, double foot) { //converting centimeter to foot double f = centimeter * 0.0328084; //converting foot to centimeter double cm = foot * 30.48; //printing result System.out.println("Value of "+centimeter+" centimeter in foot: "+ f); System.out.println("Value of "+foot+" foot in centimeter: "+ cm); } }
Output: Enter value of centimeter: 5555 Enter value of foot: 5.7 Value of 5555.0 centimeter in foot: 182.250662 Value of 5.7 foot in centimeter: 173.73600000000002
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
Related Java Programs: