Convert Temperature from Celsius to Fahrenheit in C#
Table of Contents
- Convert Temperature from Celsius to Fahrenheit in C#
- Introduction
- Objectives
- Step-by-Step Instructions with Beginner-Friendly Code
- Step 1: Prepare Your Folder
- Step 2: Open VS Code
- Step 3: Open Terminal
- Step 4: Create a New Console Project
- Step 5: Navigate to the Project Folder
- Step 6: Write the Code
- Step 7: Run the Program
- Expected Output
- Code Explanation
- Summary
- Quiz (Bloom’s Taxonomy)
- Exercises, Assessment, and Lab Exam
- Introduction to Exercises
- Exercises
- Assessment
- Lab Exam
Introduction
Temperature conversion is a common task in programming, especially in scientific, educational, and weather-related applications. For example, weather apps need to convert temperature values between Celsius and Fahrenheit depending on the user’s location and preferences.
In this lesson, we will create a simple C# program that asks the user to enter a temperature in Celsius, then converts it to Fahrenheit using the formula:
F=(C×9/5)+32F = (C \times 9/5) + 32
Where:
- F = Temperature in Fahrenheit
- C = Temperature in Celsius
This activity introduces two fundamental programming concepts:
- User Input – Getting data from the user using
Console.ReadLine()
. - Type Conversion – Converting text (string) input into a numeric data type using
Convert.ToDouble()
so we can perform mathematical calculations.
By completing this lesson, you will strengthen your skills in handling input and output, performing calculations, and displaying results. This task forms the foundation for building practical applications such as weather calculators, science lab tools, and even mobile weather apps.
Objectives
Introduction to Objectives:
The objectives are designed to help you understand, learn, practice, and apply the key concepts in a structured way. Following these stages will ensure you not only write code but also comprehend how and why it works.
By the end of this lesson, you will be able to:
- Understand – Explain how to read user input in C#, perform type conversion, and apply mathematical formulas in programming.
- Learn – Identify the correct syntax for converting Celsius to Fahrenheit and writing clear, logical code.
- Practice – Create and run a working C# console application that converts Celsius to Fahrenheit.
- Apply – Modify the program to convert Fahrenheit back to Celsius or include multiple conversions in one application.
Step-by-Step Instructions with Beginner-Friendly Code
Follow these steps carefully to create your temperature conversion program.
Step 1: Prepare Your Folder
- Open Drive D: or your preferred storage location.
- Create a folder with this format:
2025-1_lastname_bsis_1a
Example:
2025-1_doe_bsis_1a
- Inside this folder, create another folder named
celsius_to_fahrenheit
.
Step 2: Open VS Code
- Launch Visual Studio Code (VS Code).
- Go to File → Open Folder and open the folder you just created.
Step 3: Open Terminal
- In VS Code, click Terminal → New Terminal.
- The terminal window will open at the bottom of the screen.
Step 4: Create a New Console Project
In the terminal, type:
dotnet new console -n celsius_to_fahrenheit
This command generates a project folder containing:
celsius_to_fahrenheit.csproj
– Project configuration file.Program.cs
– The main C# file where we will write our code.
cd celsius_to_fahrenheit
Step 6: Write the Code
- Open the
Program.cs
file in VS Code. - Replace its contents with the code below:
using System;
namespace TemperatureConversionApp
{
class Program
{
static void Main(string[] args)
{
// Ask the user to enter temperature in Celsius
Console.Write("Enter temperature in Celsius: ");
double celsius = Convert.ToDouble(Console.ReadLine());
// Convert Celsius to Fahrenheit
double fahrenheit = (celsius * 9 / 5) + 32;
// Display the result
Console.WriteLine("The temperature in Fahrenheit is: " + fahrenheit);
}
}
}
Step 7: Run the Program
In the terminal, type:
dotnet run
Expected Output
Enter temperature in Celsius: 37
The temperature in Fahrenheit is: 98.6
Code Explanation
using System;
- Allows access to built-in C# commands such as
Console.WriteLine
andConsole.ReadLine
.
- Allows access to built-in C# commands such as
- Variable Declaration
double celsius
stores the user input.double fahrenheit
stores the calculated temperature.
- Input with
Console.ReadLine()
- Captures the user’s input as text.
- Type Conversion with
Convert.ToDouble()
- Converts the text input into a
double
so we can perform mathematical operations.
- Converts the text input into a
- The Conversion Formula
(celsius * 9 / 5) + 32
converts Celsius to Fahrenheit.
- Output with
Console.WriteLine()
- Displays the calculated Fahrenheit temperature to the user.
Summary
In this lesson, you learned how to:
- Use
Console.ReadLine()
to get user input. - Convert text input to a numerical value using
Convert.ToDouble()
. - Apply the Celsius-to-Fahrenheit formula to perform a real-world calculation.
- Display the result using
Console.WriteLine()
.
This skill is essential because many programs rely on data entry and calculations, whether for scientific applications, weather forecasting, or e-commerce pricing tools. By practicing these steps, you are building a strong foundation for developing interactive and practical software.
Quiz (Bloom’s Taxonomy)
- Which method is used to capture user input in C#?
A. Console.Input()
B. Console.ReadLine()
C. Console.Write()
D. Console.Scan() - What does
Convert.ToDouble()
do in this program?
A. Displays numbers on the screen
B. Converts text into a decimal number
C. Adds two numbers together
D. Converts numbers to text - What is the correct formula for converting Celsius to Fahrenheit?
A. (Celsius * 5 / 9) + 32
B. (Celsius * 9 / 5) + 32
C. (Celsius / 9) * 5 – 32
D. (Celsius – 32) * 5 / 9 - If the user inputs “Hello” instead of a number, what happens?
A. The program ignores it
B. The program automatically converts it to zero
C. The program throws an error
D. Nothing happens - Why is type conversion important in this lesson?
A. To make the code shorter
B. To convert text input into numbers for calculations
C. To display text on the screen
D. It is optional and not needed here
Exercises, Assessment, and Lab Exam
Introduction to Exercises
These exercises are designed to help you reinforce your understanding of user input, type conversion, and real-world calculations. By completing them, you’ll improve your problem-solving and programming skills.
Exercises
- Modify the program to convert Fahrenheit back to Celsius.
- Update the program to accept multiple temperatures and display each converted result.
- Create a program that converts Celsius to Kelvin using the formula:
Kelvin = Celsius + 273.15
- Add an if-else statement to display messages like “Freezing Point”, “Boiling Point”, or “Normal Temperature” based on the Celsius value.
- Combine Celsius-to-Fahrenheit and Fahrenheit-to-Celsius conversions in one program, letting the user choose the conversion type.
Assessment
- Written Quiz:
Answer the 5 multiple-choice questions provided above. - Performance Task:
Create a program that:- Asks the user for two temperature values in Celsius.
- Converts both to Fahrenheit.
- Displays which temperature is higher.
Lab Exam
Write a C# program that:
- Asks the user to input temperatures in Celsius, Fahrenheit, and Kelvin.
- Converts each one to the other two scales.
- Displays the results clearly.
Example Output:
Enter temperature in Celsius: 0
Fahrenheit: 32
Kelvin: 273.15
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.