➤ Check Even Number
➤ Check Odd Number
➤ Java Even-Odd
➤ Greatest of 3 numbers
➤ Exponents in Java
➤ Java Leap Year Program
➤ Display Multiplication Table
➤ Reverse of a number
➤ Factors of a Number
➤ Java LCM of 2 Numbers
➤ Java HCF of 2 Numbers
➤ Quadratic Equation Program
➤ Square Root of Number
➤ Perfect Square Program
➤ Simple Calculator Program
➤ BMI Calculator Java
➤ Factorial of a Number
➤ Factorial Using Recursion
# Java Programs to Find Sum
# Java Conversion Programs
# Java Program on Series
# Java Pattern Programs
# Java Number Programs
Java Array Programs
Java String Programs
Previously we have developed a Java program to convert the temperature from Celsius to Fahrenheit. In a similar way, we can also write Java program to convert the temperature from Fahrenheit to Celsius.
The formula that will be used for this problem is, ⁰C = ( 5 / 9 ) * ( ⁰F – 32 )
import java.util.Scanner;
public class TemperatureConversion {
// method to convert fahrenheit to celsius
public static double toCelsius(double fahrenheit){
return ((5.0/9.0) * (fahrenheit - 32));
}
public static void main(String[] args) {
// declare variables
double celsius = 0.0;
double fahrenheit = 0.0;
// create Scanner class object
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter Fahrenheit value::");
fahrenheit = scan.nextDouble();
// convert temperature
celsius = toCelsius(fahrenheit);
// display result
System.out.format("Celsius value = %.2f",
celsius);
// close Scanner class object
scan.close();
}
}
The output for different test-cases:-
Enter Fahrenheit value:: 98
Celsius value = 36.67
Enter Fahrenheit value:: 50
Celsius value = 10.00
Enter Fahrenheit value:: -40
Celsius value = -40.00
Note:- The -40⁰C is equal to the -40⁰F.
Java Program to convert Fahrenheit to Celsius In a range
Generally, we need to convert Fahrenheit to Celsius in between an interval. So, now, we will develop a Java program to convert the temperature from Celsius to Fahrenheit in between a given range with a step like Fahrenheit = 0,20,40,60,80….,200. There are various methods, some of which are given here.
import java.util.Scanner;
public class TemperatureConversionInRange {
// method to convert fahrenheit to celsius
private static double toCelsius(double fahrenheit){
return ((5.0/9.0) * (fahrenheit - 32));
}
public static void main(String[] args) {
// declare variables
int lower, upper, step;
// create Scanner class object
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter Lower limit:: ");
lower = scan.nextInt();
System.out.print("Enter Upper limit:: ");
upper = scan.nextInt();
System.out.print("Enter step:: ");
step = scan.nextInt();
// convert temperature
System.out.println("Fahrenheit \t"+
"Celsius");
for(int i=lower; i<=upper; i+=step)
// display result
System.out.format("%d \t\t %.2f\n",
i, toCelsius(i) );
// close Scanner class object
scan.close();
}
}
The output for the different test-cases:-
Enter Lower limit:: 0Enter
Upper limit:: 200
Enter step:: 20
Fahrenheit Celsius
0 -17.78
20 -6.67
40 4.44
60 15.56
80 26.67
100 37.78
120 48.89
140 60.00
160 71.11
180 82.22
200 93.33
Similar Java programming examples
- Addition Program in Java
- Java program to find Simple Interest
- Java program to find the Compound Interest
- Display ASCII value using Java
- Java program to calculate Area of Circle
- Program to find Area of Rectangle in Java
- Program to find Area of Triangle in Java
- Krishnamurthy Number in java
- Strong Number in Java
- Perfect Number
- Neon Number in Java
- Automorphic Number
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!