Conversion - C# Programming Exercise

This C# exercise teaches you how to perform temperature unit conversions. The program prompts the user to input a temperature in Celsius degrees and then converts it to Kelvin and Fahrenheit using the following formulas:

Kelvin = Celsius + 273
Fahrenheit = Celsius x 1.8 + 32

After the conversions, the program will display the equivalent temperature in both Kelvin and Fahrenheit, allowing the user to see how the input temperature changes in these two units.

This exercise is essential for understanding how to work with unit conversions in C#, and it provides a classic example of how to manipulate numeric data and perform mathematical calculations within a program. It also reinforces the use of user input and console output, key concepts in developing interactive applications in C#.

 Category

First contact with C# Sharp

 Exercise

Conversion

 Objective

Write a C# program to convert Celsius degrees to Kelvin and Fahrenheit. The program will prompt the user to input the temperature in Celsius degrees, and then use the following conversion formulas:

Kelvin = Celsius + 273
Fahrenheit = Celsius x 1.8 + 32

The program will then display the equivalent temperature in both Kelvin and Fahrenheit units.

 Write Your C# Exercise

using System; // Importing the System namespace to use Console functionalities

class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        double celsius; // Declaring a variable to store the temperature in Celsius
        double kelvin;  // Declaring a variable to store the temperature in Kelvin
        double fahrenheit; // Declaring a variable to store the temperature in Fahrenheit

        // Asking the user to enter the temperature in Celsius and reading the input
        Console.Write("Enter temperature in Celsius: ");
        celsius = Convert.ToDouble(Console.ReadLine()); // Converting the input to a double

        // Converting Celsius to Kelvin using the formula: Kelvin = Celsius + 273
        kelvin = celsius + 273; // Performing the conversion to Kelvin

        // Converting Celsius to Fahrenheit using the formula: Fahrenheit = Celsius * 1.8 + 32
        fahrenheit = celsius * 1.8 + 32; // Performing the conversion to Fahrenheit

        // Displaying the equivalent temperature in Kelvin
        Console.WriteLine("Temperature in Kelvin: {0}", kelvin); // Printing the temperature in Kelvin

        // Displaying the equivalent temperature in Fahrenheit
        Console.WriteLine("Temperature in Fahrenheit: {0}", fahrenheit); // Printing the temperature in Fahrenheit
    }
}

 Share this C# exercise

 More C# Programming Exercises of First contact with C# Sharp

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  First contact with C#

    This exercise is an excellent way to get started with programming in C#. In this program, you will learn how to print a message on the screen using C#. The program wi...

  •  Sum of two numbers

    This exercise is a great way to practice basic arithmetic operations in C#. In this program, you will learn how to perform an addition operation between two numbers, 12 and ...

  •  Division of two numbers

    This exercise is a great opportunity to practice the division operation in C#. In this program, you will learn how to divide two numbers, in this case, 24 and 5, and ...

  •  Multiple operations and precedences

    This exercise is perfect for learning how to perform complex arithmetic operations in C#. In this program, you will be asked to solve several mathematical operations using d...

  •  Multiply using variables

    This exercise is perfect for learning how to perform a multiplication in C# with numbers entered by the user. In this program, the user will be asked to input two numbers, a...

  •  Use of {0} and comments

    This exercise is great for learning how to interact with the user in C# and perform basic mathematical operations. In this program, the user will be asked to input three num...