🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Temperature conversion between Fahrenheit and Celsius is a common task in many applications. To convert Fahrenheit to Celsius, you can use the following formula:
let celsius = (fahrenheit - 32) * 5/9;
This program will help convert a given temperature from Fahrenheit to Celsius using JavaScript.
Problem Statement
Create a JavaScript program that:
- Accepts a temperature in Fahrenheit.
- Converts the temperature to Celsius.
- Returns and displays the result in Celsius.
Example:
Input:
77°FOutput:
25°CInput:
32°FOutput:
0°C
Solution Steps
- Read the Input Temperature: Provide the temperature in Fahrenheit either by user input or directly within the code.
- Convert Fahrenheit to Celsius: Use the formula
Celsius = (Fahrenheit - 32) * 5/9. - Display the Result: Print the result in Celsius.
JavaScript Program
// JavaScript Program to Convert Fahrenheit to Celsius
// Author: https://www.javaguides.net/
function convertFahrenheitToCelsius(fahrenheit) {
// Step 1: Apply the conversion formula
let celsius = (fahrenheit - 32) * 5/9;
// Step 2: Return the result
return celsius;
}
// Example input
let fahrenheitTemperature = 77;
let celsiusTemperature = convertFahrenheitToCelsius(fahrenheitTemperature);
console.log(`${fahrenheitTemperature}°F is equal to ${celsiusTemperature.toFixed(2)}°C`);
Output
77°F is equal to 25.00°C
Example with Different Input
let fahrenheitTemperature = 32;
let celsiusTemperature = convertFahrenheitToCelsius(fahrenheitTemperature);
console.log(`${fahrenheitTemperature}°F is equal to ${celsiusTemperature.toFixed(2)}°C`);
Output:
32°F is equal to 0.00°C
Explanation
Step 1: Apply the Conversion Formula
- The formula to convert Fahrenheit to Celsius is: let celsius = (fahrenheit - 32) * 5/9; This formula is used to calculate the Celsius equivalent of the Fahrenheit temperature.
Step 2: Return the Result
- The function returns the result, which is printed using
console.log(). The result is rounded to two decimal places usingtoFixed(2).
Conclusion
This JavaScript program demonstrates how to convert a temperature from Fahrenheit to Celsius using a simple mathematical formula. Temperature conversion is a basic task in many applications, and this solution provides an easy way to handle it in JavaScript.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment