🎓 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 is a common task in programming. One of the most common conversions is from Celsius to Fahrenheit. The formula to convert Celsius to Fahrenheit is:
let fahrenheit = (celsius * 9/5) + 32;
This program helps convert a given temperature in Celsius to Fahrenheit using JavaScript.
Problem Statement
Create a JavaScript program that:
- Accepts a temperature in Celsius.
- Converts the temperature to Fahrenheit.
- Returns and displays the result in Fahrenheit.
Example:
Input:
25°COutput:
77°FInput:
0°COutput:
32°F
Solution Steps
- Read the Input Temperature: Provide the temperature in Celsius either by user input or directly within the code.
- Convert Celsius to Fahrenheit: Use the formula
Fahrenheit = (Celsius * 9/5) + 32. - Display the Result: Print the result in Fahrenheit.
JavaScript Program
// JavaScript Program to Convert Celsius to Fahrenheit
// Author: https://www.javaguides.net/
function convertCelsiusToFahrenheit(celsius) {
// Step 1: Apply the conversion formula
let fahrenheit = (celsius * 9/5) + 32;
// Step 2: Return the result
return fahrenheit;
}
// Example input
let celsiusTemperature = 25;
let fahrenheitTemperature = convertCelsiusToFahrenheit(celsiusTemperature);
console.log(`${celsiusTemperature}°C is equal to ${fahrenheitTemperature}°F`);
Output
25°C is equal to 77°F
Example with Different Input
let celsiusTemperature = 0;
let fahrenheitTemperature = convertCelsiusToFahrenheit(celsiusTemperature);
console.log(`${celsiusTemperature}°C is equal to ${fahrenheitTemperature}°F`);
Output:
0°C is equal to 32°F
Explanation
Step 1: Apply the Conversion Formula
- The formula to convert Celsius to Fahrenheit is used: (celsius * 9/5) + 32; This calculates the Fahrenheit equivalent of the Celsius temperature.
Step 2: Return the Result
- The result is returned by the function and printed using
console.log().
Conclusion
This JavaScript program demonstrates how to convert a temperature from Celsius to Fahrenheit using a simple mathematical formula. Temperature conversion is a common task in many programming scenarios, and this solution provides a straightforward way to handle it.
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