Here we will Convert a temperature value from Fahrenheit into Celsius using Java 8.
- Enter the use input from Scanner(System.in) object.
- Here sc.nextDouble() reads the double-precision floating-point value.
- DoubleStream.of(double..values)perform calculations and generating statistics.
- Inside map function use formula r -> ((r – 32) * 5) / 9 to calculate the temperature in Celsius double.
- prints the calculated Celsius temperature.
import java.util.Scanner;
import java.util.stream.DoubleStream;
public class Hello {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter temperature in Fahrenheit: ");
double fahrenheit = input.nextDouble();
DoubleStream celsius = DoubleStream.of(fahrenheit).map(r -> ((r - 32) * 5) / 9);
celsius.forEach(System.out::println);
input.close();
}
}
Output :-
Enter temperature in Fahrenheit:
50
10.0