w3resource

C++ Exercises: Convert temperature in Celsius to Kelvin


Celsius to Kelvin Conversion

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

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 Celsius to Kelvin :\n"; // Outputting a message indicating temperature conversion
    cout << "---------------------------------------------------\n"; // Outputting a separator line

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

    kel = cel + 273.15; // Converting Celsius to Kelvin by adding the Celsius temperature to 273.15 (absolute zero in Celsius)

    cout << " The temperature in Celsius : " << cel << endl; // Displaying the input temperature in Celsius
    cout << " The temperature in Kelvin : " << kel << endl; // Displaying the converted temperature in Kelvin
    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 Celsius to Kelvin  :                           
---------------------------------------------------                    
 Input the temperature in Celsius : 26.85                              
 The temperature in Celsius : 26.85                                    
 The temperature in Kelvin : 300  

Flowchart:

Flowchart: Convert temperature in Celsius to Kelvin

For more Practice: Solve these Related Problems:

  • Write a C++ program to convert Celsius to Kelvin ensuring that the input is above absolute zero.
  • Write a C++ program that uses a user-defined function to convert Celsius to Kelvin and prints the output with fixed notation.
  • Write a C++ program to convert a series of Celsius temperatures to Kelvin and display the conversions in a formatted list.
  • Write a C++ program that reads Celsius values, converts them to Kelvin, and then checks if the resulting temperature qualifies as room temperature.

Go to:


PREV : Fahrenheit to Kelvin Conversion.
NEXT : Scalene Triangle Area Calculation.

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.