In this article, we will learn how to convert Celsius to Fahrenheit and vice-versa
1. Convert Celsius to Fahrenheit :
- Single conversion using straight calculation
- Multiple conversion using Java 8 Stream
1.1 Celsius to Fahrenheit using straight calculation :
- Formula for Celsius to Fahrenheit conversion is
- fahrenheit = (celsius * 1.8) + 32
ConvertCelsiusToFahrenheit.java
package in.bench.resources.arrays.find.number;
public class ConvertCelsiusToFahrenheit {
public static void main(String[] args) {
// local variables
double celsius = 30.0; // Thirty degree celsius
double fahrenheit = 0.0;
// convert Celsius to Fahrenheit
fahrenheit = (celsius * 1.8) + 32;
// print to console
System.out.println("Temperature in Degree Celsius is = " + celsius);
System.out.print("Celsius(" + celsius + ") to Fahrenheit is = " + fahrenheit);
}
}
Output:
Temperature in Degree Celsius is = 30.0
Celsius(30.0) to Fahrenheit is = 86.0
1.2 Celsius to Fahrenheit using Java 8 Stream :
- A list contains degree Celsius of different cities which needs to be converted to Fahrenheit
- Get the stream for list and use Stream.mapToDouble() method to convert Celsius to Fahrenheit
ConvertCelsiusToFahrenheitUsingJava8.java
package in.bench.resources.arrays.find.number;
import java.util.ArrayList;
import java.util.List;
public class ConvertCelsiusToFahrenheitUsingJava8 {
public static void main(String[] args) {
// create List to store Celsius
List<Double> degreeCelsius = new ArrayList<>();
// add degree celsius of different cities
degreeCelsius.add(30d); // mumbai
degreeCelsius.add(27d); // delhi
degreeCelsius.add(32d); // kolkata
degreeCelsius.add(34d); // chennai
degreeCelsius.add(31d); // lucknow
// Java 8 Stream - convert Celsius to Fahrenheit
degreeCelsius
.stream()
.peek(celsius -> System.out.print("Temperature in Celsius is = " + celsius))
.mapToDouble(celsius -> (celsius * 1.8) + 32)
.forEach(fahrenheit -> System.out.println("\tTemperature in Fahrenheit is = " + fahrenheit));
}
}
Output:
Temperature in Celsius is = 30.0 Temperature in Fahrenheit is = 86.0
Temperature in Celsius is = 27.0 Temperature in Fahrenheit is = 80.6
Temperature in Celsius is = 32.0 Temperature in Fahrenheit is = 89.6
Temperature in Celsius is = 34.0 Temperature in Fahrenheit is = 93.2
Temperature in Celsius is = 31.0 Temperature in Fahrenheit is = 87.80000000000001
2. Convert Fahrenheit to Celsius :
- Single conversion using straight calculation
- Multiple conversion using Java 8 Stream
2.1 Fahrenheit to Celsius using straight calculation :
- Formula for Fahrenheit to Celsius conversion is
- celsius = (fahrenheit – 32) / 1.8
ConvertFahrenheitToCelsius.java
package in.bench.resources.arrays.find.number;
public class ConvertFahrenheitToCelsius {
public static void main(String[] args) {
// local variables
double celsius = 0.0;
double fahrenheit = 96.0; // Ninety-six degree Fahrenheit
// convert Fahrenheit to Celsius
celsius = (fahrenheit - 32) / 1.8;
// print to console
System.out.println("Temperature in Degree Fahrenheit is = " + fahrenheit);
System.out.print("Fahrenheit(" + fahrenheit + ") to Celsius is = " + celsius);
}
}
Output:
Temperature in Degree Fahrenheit is = 96.0
Fahrenheit(96.0) to Celsius is = 35.55555555555556
2.2 Fahrenheit to Celsius using Java 8 Stream :
- A list contains degree Fahrenheit of different cities which needs to be converted to Celsius
- Get the stream for list and use Stream.mapToDouble() method to convert Fahrenheit to Celsius
ConvertFahrenheitToCelsiusUsingJava8.java
package in.bench.resources.arrays.find.number;
import java.util.ArrayList;
import java.util.List;
public class ConvertFahrenheitToCelsiusUsingJava8 {
public static void main(String[] args) {
// create List to store Fahrenheit
List<Double> degreeFahrenheit = new ArrayList<>();
// add degree fahrenheit of different cities
degreeFahrenheit.add(86d); // mumbai
degreeFahrenheit.add(81d); // delhi
degreeFahrenheit.add(90d); // kolkata
degreeFahrenheit.add(94d); // chennai
degreeFahrenheit.add(88d); // lucknow
// Java 8 Stream - convert Fahrenheit to Celsius
degreeFahrenheit
.stream()
.peek(fahrenheit -> System.out.print("Temperature in Fahrenheit is = " + fahrenheit))
.mapToDouble(fahrenheit -> (fahrenheit - 32) / 1.8)
.forEach(celsius -> System.out.println("\tTemperature in Celsius is = " + celsius));
}
}
Output:
Temperature in Fahrenheit is = 86.0 Temperature in Celsius is = 30.0
Temperature in Fahrenheit is = 81.0 Temperature in Celsius is = 27.22222222222222
Temperature in Fahrenheit is = 90.0 Temperature in Celsius is = 32.22222222222222
Temperature in Fahrenheit is = 94.0 Temperature in Celsius is = 34.44444444444444
Temperature in Fahrenheit is = 88.0 Temperature in Celsius is = 31.11111111111111
Related Articles :
- Java – To print first N natural numbers
- Java – Swapping two numbers without temporary variable
- Java – Swapping two numbers using third or temporary variable
- Java – Check whether the given number is Armstrong number or Not ?
- Java – Check whether number is Positive or Negative or Zero ?
- Java – Check whether number is Even or Odd ?
- Java – How to Reverse a Number in different ways ?
- Java – Check whether number is Prime or Not ?
- Java – Print Prime numbers between specified range or interval
- Java – How to find/remove first and last digit of a number ?
- Java – How to generate Fibonacci numbers using Stream ?
- Java – How to convert Roman letter/numbers to Integer ?
- Java – How to convert Celsius to Fahrenheit and vice-versa ?
References :
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
- https://docs.oracle.com/javase/8/docs/api/java/util/List.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
Happy Coding !!
Happy Learning !!