Java Code Example: convert celsius to fahrenheit

This code defines a Java program that converts a temperature in Celsius to Fahrenheit.

The code starts by importing the Scanner class from the java.util package. The Scanner class is used to take input from the user.

The main method then declares two variables: c to store the temperature in Celsius, and f to store the converted temperature in Fahrenheit.

A Scanner object s is created to take input from the user. The program then prompts the user to enter the temperature in Celsius. The input is stored in the c variable by calling the nextDouble method on the Scanner object.

The conversion from Celsius to Fahrenheit is then performed using the formula f = c * 1.8 + 32. The result of the calculation is stored in the f variable.

Finally, the program closes the Scanner object by calling the close method and outputs the converted temperature in Fahrenheit by printing the value of f to the console.

import java.util.Scanner;

public class ConvertCelsiusToFahrenheit {
  public static void main(String[] args) {
    double c, f;

    Scanner s = new Scanner(System.in);
    System.out.print("Please enter temperature in celsius: ");

    c = s.nextDouble();
    f = c * 1.8 + 32;

    s.close();

    System.out.print("Temperature in fahrenheit: " + f);
  }
}
Output
Please enter temperature in celsius: 10
Temperature in fahrenheit: 50.0