In the previous article, we have discussed about Java Program to Convert Celsius to kelvin and Kelvin to Celsius
In this article we will see how to convert Fahrenheit to Kelvin and Kelvin to Fahrenheit by using Java programming language.
Java Program to Convert Fahrenheit to Kelvin and Kelvin to Fahrenheit
Before jumping into the program let’s know the relationship between Fahrenheit and Kelvin and how we can convert Fahrenheit to Kelvin and vice versa.
Generally Fahrenheit and Kelvin are used as unit in case of temperature measurement.
1 Fahrenheit = 255.928 Kelvin 1 Kelvin = -457.87 Fahrenheit
Formula to convert Fahrenheit to Kelvin.
Kelvin = ((Fahrenheit - 32) * 5 / 9) + 273.15
Formula to convert Kelvin to Fahrenheit .
Fahrenheit = (((Kelvin - 273.15) * 9) / 5) + 32
Let’s see different ways to convert Fahrenheit to Kelvin and Kelvin to Fahrenheit.
Method-1: Java Program to Convert Fahrenheit to Kelvin and Kelvin to Fahrenheit By Using Static Input Value
Approach:
- Declare Fahrenheit and Kelvin value.
- Then convert Celsius to Kelvin and Kelvin to Celsius by using the formula.
- Print result.
Program:
public class Main { public static void main(String args[]) { //value of kelvin declared double kelvin = 1; //value of fahrenheit declared double fahrenheit = 1; //converting fahrenheit to kelvin double k = (((fahrenheit - 32) * 5 / 9) + 273.15); //converting kelvin to fahrenheit double f = ((((kelvin - 273.15) * 9) / 5) + 32); //printing result System.out.println("Value of "+kelvin+" kelvin in fahrenheit: "+ f); System.out.println("Value of "+fahrenheit+" fahrenheit in kelvin: "+ k); } }
Output: Value of 1.0 kelvin in fahrenheit: -457.87 Value of 1.0 fahrenheit in kelvin: 255.92777777777775
Method-2: Java Program to Convert Fahrenheit to Kelvin and Kelvin to Fahrenheit By Using User Input Value
Approach:
- Take user input of Fahrenheit and Kelvin value.
- Then convert Fahrenheit to Kelvin and Kelvin to Fahrenheit 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 fahrenheit System.out.println("Enter value of Fahrenheit: "); double fahrenheit = sc.nextDouble(); //Taking the value input of double variable kelvin System.out.println("Enter value of kelvin: "); double kelvin = sc.nextDouble(); //converting fahrenheit to kelvin double k = (((fahrenheit - 32) * 5 / 9) + 273.15); //converting kelvin to fahrenheit double f = ((((kelvin - 273.15) * 9) / 5) + 32); //printing result System.out.println("Value of "+kelvin+" kelvin in fahrenheit: "+ f); System.out.println("Value of "+fahrenheit+" fahrenheit in kelvin: "+ k); } }
Output: Enter value of Fahrenheit: 2.5 Enter value of kelvin: 300 Value of 300.0 kelvin in fahrenheit: 80.33000000000004 Value of 2.5 fahrenheit in kelvin: 256.76111111111106
Method-3: Java Program to Convert Fahrenheit to Kelvin and Kelvin to Fahrenheit By Using User Defined Method
Approach:
- Take user input of Fahrenheit and Kelvin value.
- Call a user defined method by passing Fahrenheit and Kelvin value as parameter.
- Inside method convert Fahrenheit to Kelvin and vice versa 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 fahrenheit System.out.println("Enter value of Fahrenheit: "); double fahrenheit = sc.nextDouble(); //Taking the value input of double variable kelvin System.out.println("Enter value of kelvin: "); double kelvin = sc.nextDouble(); //calling user defined method convert() convert(fahrenheit, kelvin); } //convert() method to convert fahrenheit to Kelvin and vice versa public static void convert(double fahrenheit, double kelvin) { //converting fahrenheit to kelvin double k = (((fahrenheit - 32) * 5 / 9) + 273.15); //converting kelvin to fahrenheit double f = ((((kelvin - 273.15) * 9) / 5) + 32); //printing result System.out.println("Value of "+kelvin+" kelvin in fahrenheit: "+ f); System.out.println("Value of "+fahrenheit+" fahrenheit in kelvin: "+ k); } }
Output: Enter value of Fahrenheit: 10 Enter value of kelvin: 10 Value of 10.0 kelvin in fahrenheit: -441.66999999999996 Value of 10.0 fahrenheit in kelvin: 260.92777777777775
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Related Java Programs: