w3resource

C++ Exercises: Convert temperature in Kelvin to Celsius


Kelvin to Celsius Conversion

Write a program in C++ to convert temperature in Kelvin to Celsius.

Visual Presentation:

C++ Exercises: Convert temperature in Kelvin to Celsius

Sample Solution:

C++ Code :

#include <iostream> // Including the input-output stream header file

using namespace std; // Using the standard namespace

int main() // Start of the main function
{
    float kel, cel; // Declaring floating-point variables for Kelvin and Celsius temperatures

    cout << "\n\n Convert temperature in Kelvin to Celsius :\n"; // Outputting a message indicating temperature conversion
    cout << "------------------------------------------------\n"; // Outputting a separator line

    cout << " Input the temperature in Kelvin : "; // Prompting the user to input the temperature in Kelvin
    cin >> kel; // Taking input for the temperature in Kelvin from the user

    cel = kel - 273.15; // Converting Kelvin to Celsius using the conversion formula

    cout << " The temperature in Kelvin    : " << kel << endl; // Displaying the input temperature in Kelvin
    cout << " The temperature in Celsius : " << cel << endl; // Displaying the converted temperature in Celsius
    cout << endl; // Outputting a blank line for better readability

    return 0; // Returning 0 to indicate successful program execution
} // End of the main function

Sample Output:

 Convert temperature in Kelvin to Celsius  :                           
------------------------------------------------                       
 Input the temperature in Kelvin : 300                                 
 The temperature in Kelvin    : 300                                    
 The temperature in Celsius : 26.85 

Flowchart:

Flowchart: Convert temperature in Kelvin to Celsius

For more Practice: Solve these Related Problems:

  • Write a C++ program to convert a temperature from Kelvin to Celsius and handle potential invalid inputs.
  • Write a C++ program that uses a user-defined function to convert Kelvin to Celsius and then prints the result with two decimals.
  • Write a C++ program to convert Kelvin to Celsius and then compare the result to a predefined temperature threshold.
  • Write a C++ program that reads multiple Kelvin values, converts them to Celsius, and outputs the conversion in a table format.

Go to:


PREV : Kelvin to Fahrenheit Conversion.
NEXT : Fahrenheit to Kelvin Conversion.

C++ Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.