(Convert Celsius to Fahrenheit) Write a
fahrenheit = (9 / 5) * celsius + 32
Hint: In Java, 9 / 5 is 1, but 9.0 / 5 is 1.8.
Here is a sample run:
Convert Celsius to Fahrenheit
Program Plan:
- Define the class
- Define the main method using public static main.
- Declare the required variables.
- Prompt the user to enter Celsius value.
- Read the input
- Convert it into Fahrenheit value.
- Display the result on the screen.
- Define the main method using public static main.
The below program is used to convert Celsius to Fahrenheit.
Explanation of Solution
Program:
//class definition
public class Cel_Far
{
//Main method
public static void main(String[] args)
{
//declare scanner variable
Scanner in = new Scanner(System.in);
// Prompt the user to enter Celsius
System.out.print("Enter a temperature in Celsius: ");
//read the input
double celsius = in.nextDouble();
// Convert Celsius to Fahrenheit
double fh = (9.0 / 5) * celsius + 32;
// Display the result on the screen
System.out.println(celsius + " Celsius is " +
fh + " Fahrenheit");
}
}
Enter a temperature in Celsius: 43.5
43.5 Celsius is 110.3 Fahrenheit
Want to see more full solutions like this?
Chapter 2 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version, Student Value Edition (11th Edition)
Additional Engineering Textbook Solutions
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Java: An Introduction to Problem Solving and Programming (7th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Computer Systems: A Programmer's Perspective (3rd Edition)
Web Development and Design Foundations with HTML5 (8th Edition)
Problem Solving with C++ (9th Edition)
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning